Author: centic
Date: Sun Mar 13 21:30:18 2016
New Revision: 1734861

URL: http://svn.apache.org/viewvc?rev=1734861&view=rev
Log:
Bug 55668: Try to avoid NullPointerException when chaning cell type and formula 
leads to null-string by seting the cell to BLANK instead

Added:
    poi/trunk/test-data/spreadsheet/55668.xls
Modified:
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java?rev=1734861&r1=1734860&r2=1734861&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java Sun Mar 13 
21:30:18 2016
@@ -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()

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java?rev=1734861&r1=1734860&r2=1734861&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java 
(original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java Sun Mar 
13 21:30:18 2016
@@ -2977,4 +2977,28 @@ public final class TestBugs extends Base
 
         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();
+    }
 }

Added: poi/trunk/test-data/spreadsheet/55668.xls
URL: 
http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/55668.xls?rev=1734861&view=auto
==============================================================================
Binary files poi/trunk/test-data/spreadsheet/55668.xls (added) and 
poi/trunk/test-data/spreadsheet/55668.xls Sun Mar 13 21:30:18 2016 differ



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to