From: Nick Burch Date: Thu, 11 Feb 2016 15:13:40 +0000 (+0000) Subject: Fix #57034 on SXSSF, and add a common unit test to show it was already fixed on the... X-Git-Tag: REL_3_14_FINAL~45 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6f46cd95be3ab90a8b85f839e399761c6d637baf;p=poi.git Fix #57034 on SXSSF, and add a common unit test to show it was already fixed on the others + is now fixed for SXSSF git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1729849 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java index 7c0bfbe432..9803f0d9ec 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java @@ -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); } /** diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java index 38c2f5ca10..013e41b5f9 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java @@ -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(); + } }