]> source.dussan.org Git - poi.git/commitdiff
fixed bug #45322: HSSFSheet.autoSizeColumn() throws NPE when cell number format was...
authorYegor Kozlov <yegor@apache.org>
Tue, 15 Jul 2008 18:13:50 +0000 (18:13 +0000)
committerYegor Kozlov <yegor@apache.org>
Tue, 15 Jul 2008 18:13:50 +0000 (18:13 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@676995 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java
src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/testcases/org/apache/poi/hssf/data/45322.xls [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java

index 15f1a29d5e2cbc01f8631e6b0359246fb1a15365..12f62cb8b9a504bcb4708f00883443d0e4a90c15 100644 (file)
@@ -37,6 +37,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.1.1-alpha1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">45322 - Fixed NPE in HSSFSheet.autoSizeColumn() when cell number format was not found</action>
            <action dev="POI-DEVELOPERS" type="add">45380 - Missing return keyword in ArrayPtg.toFormulaString()</action>
            <action dev="POI-DEVELOPERS" type="add">44958 - Record level support for Data Tables. (No formula parser support though)</action>
            <action dev="POI-DEVELOPERS" type="add">35583 - Include a version class, org.apache.poi.Version, to allow easy introspection of the POI version</action>
index b77bcae69f4af766a248849e9e9ebf29787559c0..a62ee3ddebc3496da69febdaf76a395e0818c0c9 100644 (file)
@@ -34,6 +34,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.1.1-alpha1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">45322 - Fixed NPE in HSSFSheet.autoSizeColumn() when cell number format was not found</action>
            <action dev="POI-DEVELOPERS" type="add">45380 - Missing return keyword in ArrayPtg.toFormulaString()</action>
            <action dev="POI-DEVELOPERS" type="add">44958 - Record level support for Data Tables. (No formula parser support though)</action>
            <action dev="POI-DEVELOPERS" type="add">35583 - Include a version class, org.apache.poi.Version, to allow easy introspection of the POI version</action>
index c613225ba70ed606db6056f3381db5f13f3e3328..6d8ae33b689416b30afb8005985156db00bd6e97 100644 (file)
@@ -278,21 +278,23 @@ public class HSSFCellStyle
      * Get the contents of the format string, by looking up
      *  the DataFormat against the bound workbook
      * @see org.apache.poi.hssf.usermodel.HSSFDataFormat
+     * @return the format string or "General" if not found
      */
     public String getDataFormatString() {
-       HSSFDataFormat format = new HSSFDataFormat(workbook);
-       
-        return format.getFormat(getDataFormat());
+        return getDataFormatString(workbook);
     }
     /**
      * Get the contents of the format string, by looking up
      *  the DataFormat against the supplied workbook
      * @see org.apache.poi.hssf.usermodel.HSSFDataFormat
+     *
+     * @return the format string or "General" if not found
      */
     public String getDataFormatString(Workbook workbook) {
        HSSFDataFormat format = new HSSFDataFormat(workbook);
        
-        return format.getFormat(getDataFormat());
+        int idx = getDataFormat();
+        return idx == -1 ? "General" : format.getFormat(getDataFormat());
     }
 
     /**
index 2b6ad41397b643ce044c4d6a7cafab1c6d18e523..e2973df232f2f77e9ad9fe30dbde1db2d22c1d3f 100644 (file)
@@ -1851,9 +1851,7 @@ public final class HSSFSheet {
             } else {
                 String sval = null;
                 if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
-                    HSSFDataFormat dataformat = wb.createDataFormat();
-                    short idx = style.getDataFormat();
-                    String format = dataformat.getFormat(idx).replaceAll("\"", "");
+                    String format = style.getDataFormatString().replaceAll("\"", "");
                     double value = cell.getNumericCellValue();
                     try {
                         NumberFormat fmt;
diff --git a/src/testcases/org/apache/poi/hssf/data/45322.xls b/src/testcases/org/apache/poi/hssf/data/45322.xls
new file mode 100644 (file)
index 0000000..7117214
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/45322.xls differ
index 1d65b07f6a90f6be7d92849c701387626b113aa8..6e92f6e3deff0ca90f54a80c0d1c07befb2564c5 100644 (file)
@@ -1353,4 +1353,13 @@ public final class TestBugs extends TestCase {
         // TODO - check the formula once tables and
         //  arrays are properly supported
     }
+
+    /**
+     * 45322: HSSFSheet.autoSizeColumn fails when style.getDataFormat() returns -1
+     */
+    public void test45322() throws Exception {
+        HSSFWorkbook wb = openSample("44958.xls");
+        HSSFSheet sh = wb.getSheetAt(0);
+        for(short i=0; i < 30; i++) sh.autoSizeColumn(i);
+     }
 }