]> source.dussan.org Git - poi.git/commitdiff
Bug 55668: Try to avoid NullPointerException when chaning cell type and formula leads...
authorDominik Stadler <centic@apache.org>
Sun, 13 Mar 2016 21:30:18 +0000 (21:30 +0000)
committerDominik Stadler <centic@apache.org>
Sun, 13 Mar 2016 21:30:18 +0000 (21:30 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734861 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
test-data/spreadsheet/55668.xls [new file with mode: 0644]

index 407f15b47e782c2dd02a1f84d2bd34fab6e5563f..e31f2f5808185fe1f361616ce6c06d364975af51 100644 (file)
@@ -346,11 +346,18 @@ public class HSSFCell implements Cell {
                 }
                 if (setValue) {
                     String str = convertCellValueToString();
-                    int sstIndex = _book.getWorkbook().addSSTString(new UnicodeString(str));
-                    lrec.setSSTIndex(sstIndex);
-                    UnicodeString us = _book.getWorkbook().getSSTString(sstIndex);
-                    _stringValue = new HSSFRichTextString();
-                    _stringValue.setUnicodeString(us);
+                    if(str == null) {
+                        // bug 55668: don't try to store null-string when formula
+                        // results in empty/null value
+                        setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
+                        return;
+                    } else {
+                        int sstIndex = _book.getWorkbook().addSSTString(new UnicodeString(str));
+                        lrec.setSSTIndex(sstIndex);
+                        UnicodeString us = _book.getWorkbook().getSSTString(sstIndex);
+                        _stringValue = new HSSFRichTextString();
+                        _stringValue.setUnicodeString(us);
+                    }
                 }
                 _record = lrec;
                 break;
@@ -884,7 +891,7 @@ public class HSSFCell implements Cell {
      * the HSSFWorkbook.</p>
      * 
      * <p>To change the style of a cell without affecting other cells that use the same style,
-     * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(org.apache.poi.ss.usermodel.Cell, Map)}</p>
+     * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(org.apache.poi.ss.usermodel.Cell, java.util.Map)}</p>
      *
      * @param style  reference contained in the workbook
      * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle()
index 078008a899d7bc6453f36d0ac31c5b5d1a52452d..7cc9b4ea59508cec275a59ce23889c07ea2615a3 100644 (file)
@@ -2977,4 +2977,28 @@ public final class TestBugs extends BaseTestBugzillaIssues {
 
         wb.close();
     }
+
+    @Test
+    public void test55668() throws IOException {
+        Workbook wb = HSSFTestDataSamples.openSampleWorkbook("55668.xls");
+
+        Sheet sheet = wb.getSheetAt(0);
+        Row row = sheet.getRow(0);
+        Cell cell = row.getCell(0);
+        assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
+        assertEquals("IF(TRUE,\"\",\"\")", cell.getCellFormula());
+        assertEquals("", cell.getStringCellValue());
+        cell.setCellType(Cell.CELL_TYPE_STRING);
+
+        assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType());
+        try {
+            assertNull(cell.getCellFormula());
+            fail("Should throw an exception here");
+        } catch (IllegalStateException e) {
+            // expected here
+        }
+        assertEquals("", cell.getStringCellValue());
+
+        wb.close();
+    }
 }
diff --git a/test-data/spreadsheet/55668.xls b/test-data/spreadsheet/55668.xls
new file mode 100644 (file)
index 0000000..8aa8f50
Binary files /dev/null and b/test-data/spreadsheet/55668.xls differ