You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by bu...@apache.org on 2019/03/07 14:59:52 UTC

[Bug 63242] New: NullPointerException when trying to write a boxed Integer null value to a cell using setCellValue due to automatic unboxing

https://bz.apache.org/bugzilla/show_bug.cgi?id=63242

            Bug ID: 63242
           Summary: NullPointerException when trying to write a boxed
                    Integer null value to a cell using setCellValue due to
                    automatic unboxing
           Product: POI
           Version: 4.0.0-FINAL
          Hardware: PC
            Status: NEW
          Severity: normal
          Priority: P2
         Component: XSSF
          Assignee: dev@poi.apache.org
          Reporter: remco.siemonsma@gmail.com
  Target Milestone: ---

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.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


[Bug 63242] NullPointerException when trying to write a boxed Integer null value to a cell using setCellValue due to automatic unboxing

Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=63242

Dominik Stadler <do...@gmx.at> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 OS|                            |All
         Resolution|---                         |INVALID

--- Comment #1 from Dominik Stadler <do...@gmx.at> ---
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.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org