Bug 58113

Summary: calling setCellValue with a null String object gives null pointer exception
Product: POI Reporter: scottd
Component: SXSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: regression    
Priority: P2    
Version: 3.12-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   

Description scottd 2015-07-07 20:18:01 UTC
Test code below works fine in version 3.11, gives null pointer exception in 3.12:

Workbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet( "Test" );

Row row = sheet.createRow( 0 );
row.setHeightInPoints( 16.00f );

Cell cell = row.createCell( 0 );
String str = null;
cell.setCellValue( str );

According to the documentation null should be a valid: 

"value - value to set the cell to. For formulas we'll set the formula string, for String cells we'll set its value. For other types we will change the cell to a string cell and set its value. If value is null then we will change the cell to a Blank cell."

After downloading the source for 3.11 and 3.12, the problem is pretty clear, it's now trying to check the length of value without checking if it's null first.

3.11:
public void setCellValue(String value)
{
    ensureTypeOrFormulaType(CELL_TYPE_STRING);
    if(_value.getType()==CELL_TYPE_FORMULA)
        ((StringFormulaValue)_value).setPreEvaluatedValue(value);
    else
        ((PlainStringValue)_value).setValue(value);
}

3.12:
public void setCellValue(String value)
{
    ensureTypeOrFormulaType(CELL_TYPE_STRING);
        
    if(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);
}
Comment 1 Dominik Stadler 2015-07-13 12:58:47 UTC
Fixed via r1690652, we now allow null-values again and have unit tests for all variants (HSSF, XSSF, SXSSF) which verify that it works.