]> source.dussan.org Git - poi.git/commitdiff
Fix #57034 on SXSSF, and add a common unit test to show it was already fixed on the...
authorNick Burch <nick@apache.org>
Thu, 11 Feb 2016 15:13:40 +0000 (15:13 +0000)
committerNick Burch <nick@apache.org>
Thu, 11 Feb 2016 15:13:40 +0000 (15:13 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1729849 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java

index 7c0bfbe43230ae97bad98c6ca95e5ccc0ce51c7e..9803f0d9ec4674dcee05a092c5659a7b6c5fa8e2 100644 (file)
@@ -245,7 +245,7 @@ public class SXSSFCell implements Cell {
     {
         XSSFRichTextString xvalue = (XSSFRichTextString)value;
         
-        if (xvalue != null) {
+        if (xvalue != null && xvalue.getString() != null) {
             ensureRichTextStringType();
             
             if (xvalue.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) {
@@ -271,16 +271,20 @@ public class SXSSFCell implements Cell {
     @Override
     public void setCellValue(String value)
     {
-        ensureTypeOrFormulaType(CELL_TYPE_STRING);
-        
-        if(value != null && value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()){
-            throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
+        if (value != null) {
+            ensureTypeOrFormulaType(CELL_TYPE_STRING);
+            
+            if(value != null && value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()){
+                throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
+            }
+    
+            if(_value.getType()==CELL_TYPE_FORMULA)
+                ((StringFormulaValue)_value).setPreEvaluatedValue(value);
+            else
+                ((PlainStringValue)_value).setValue(value);
+        } else {
+            setCellType(CELL_TYPE_BLANK);
         }
-
-        if(_value.getType()==CELL_TYPE_FORMULA)
-            ((StringFormulaValue)_value).setPreEvaluatedValue(value);
-        else
-            ((PlainStringValue)_value).setValue(value);
     }
 
     /**
index 38c2f5ca10b7e1da20adedf983865591b23717e3..013e41b5f90f7bc1d5fc58b5b760faea99dd2ec6 100644 (file)
@@ -1380,4 +1380,27 @@ public abstract class BaseTestBugzillaIssues {
 
         wb.close();
     }
+    
+    /**
+     * If someone sets a null string as a cell value, treat
+     *  it as an empty cell, and avoid a NPE on auto-sizing
+     */
+    @Test
+    public void test57034() throws Exception {
+        Workbook wb = _testDataProvider.createWorkbook();
+        Sheet s = wb.createSheet();
+        Cell cell = s.createRow(0).createCell(0);
+        cell.setCellValue((String)null);
+        assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType());
+        
+        _testDataProvider.trackColumnsForAutosizing(s, 0);
+        
+        s.autoSizeColumn(0);
+        assertEquals(2048, s.getColumnWidth(0));
+
+        s.autoSizeColumn(0, true);
+        assertEquals(2048, s.getColumnWidth(0));
+
+        wb.close();
+    }
 }