From: Yegor Kozlov Date: Tue, 15 Jul 2008 18:13:50 +0000 (+0000) Subject: fixed bug #45322: HSSFSheet.autoSizeColumn() throws NPE when cell number format was... X-Git-Tag: REL_3_2_FINAL~240 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6b6f924c299bae2ee80c1aee644a9c174a75c39b;p=poi.git fixed bug #45322: HSSFSheet.autoSizeColumn() throws NPE when cell number format was not found git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@676995 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 15f1a29d5e..12f62cb8b9 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 45322 - Fixed NPE in HSSFSheet.autoSizeColumn() when cell number format was not found 45380 - Missing return keyword in ArrayPtg.toFormulaString() 44958 - Record level support for Data Tables. (No formula parser support though) 35583 - Include a version class, org.apache.poi.Version, to allow easy introspection of the POI version diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index b77bcae69f..a62ee3ddeb 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 45322 - Fixed NPE in HSSFSheet.autoSizeColumn() when cell number format was not found 45380 - Missing return keyword in ArrayPtg.toFormulaString() 44958 - Record level support for Data Tables. (No formula parser support though) 35583 - Include a version class, org.apache.poi.Version, to allow easy introspection of the POI version diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index c613225ba7..6d8ae33b68 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -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()); } /** diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index 2b6ad41397..e2973df232 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -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 index 0000000000..711721400e Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/45322.xls differ diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index 1d65b07f6a..6e92f6e3de 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -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); + } }