From 2776cf6a599ebecb5d135e4e05d7fc995b46ad4a Mon Sep 17 00:00:00 2001
From: Dominik Stadler
Date: Sun, 13 Mar 2016 21:30:18 +0000
Subject: [PATCH] Bug 55668: Try to avoid NullPointerException when chaning
cell type and formula leads to null-string by seting the cell to BLANK
instead
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734861 13f79535-47bb-0310-9956-ffa450edef68
---
.../apache/poi/hssf/usermodel/HSSFCell.java | 19 +++++++++-----
.../apache/poi/hssf/usermodel/TestBugs.java | 24 ++++++++++++++++++
test-data/spreadsheet/55668.xls | Bin 0 -> 23040 bytes
3 files changed, 37 insertions(+), 6 deletions(-)
create mode 100644 test-data/spreadsheet/55668.xls
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
index 407f15b47e..e31f2f5808 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
@@ -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.
*
* 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)}
+ * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(org.apache.poi.ss.usermodel.Cell, java.util.Map)}
*
* @param style reference contained in the workbook
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle()
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
index 078008a899..7cc9b4ea59 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
@@ -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
index 0000000000000000000000000000000000000000..8aa8f5042767b04d4162e8c2cd7c6b96e6831366
GIT binary patch
literal 23040
zcmeHPdu$xV8UJ>DXRqTte0ezU)X3!awH|TCQcb#f0wAGZP
zZt9|K)Q9u`Qi&a!MwigDtfai8yK-_Sg`~|cCG2ep4O`+2bfj{zW3a{|!)k2&uqBob
zTVnYzB{WJX`cNg+sCAeUgcAO}uTkZ&7$b(k&@rZl<6;cUrP32)M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