Bug 63242 - NullPointerException when trying to write a boxed Integer null value to a cell using setCellValue due to automatic unboxing
Summary: NullPointerException when trying to write a boxed Integer null value to a cel...
Status: RESOLVED INVALID
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 4.0.0-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-03-07 14:59 UTC by Remco Siemonsma
Modified: 2019-03-07 16:24 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Remco Siemonsma 2019-03-07 14:59:52 UTC
Test code below leads to a NPE during runtime due to the way the Java compiler handles unboxing during compilation/linking.

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Main {
    public static void main(String[] args) throws Exception {
        Integer nullint = null;

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Sheet");

        XSSFRow row = sheet.createRow(0);

        XSSFCell nullCell = row.createCell(0);
        nullCell.setCellValue(nullint);
    }
}

Since the XSSFCell does not provide a method which accepts boxed values, or an Object, the Java compiler decides the "setCellValue(double value)" method is the best method to link to. At runtime the JVM tries to unbox the null Integer, or any other numeric value, and throws an NPE.

Currently, a workaround can be done by first manually checking whether the value being written is null before the call to setCellValue, but this should not be needed.
Comment 1 Dominik Stadler 2019-03-07 16:24:49 UTC
Sorry, but this is how the API is intended, not a problem that we want to fix in Apache POI itself. Please adjust your code so you do cause a NullPointerException in your code.

We want to ensure that we get a valid integer value in the setCellValue() anyway, a null would be invalid and the only way to handle it would be to throw an exception, so you need to handle this before calling the interface here.