Bug 59199 - setting null value to date cell leads to NPE
Summary: setting null value to date cell leads to NPE
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.14-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-03-18 14:08 UTC by jupp.strunk
Modified: 2016-03-28 20:26 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description jupp.strunk 2016-03-18 14:08:24 UTC
When setting a Date value in a cell and the date is null, a NullPointerException is thrown.

Example:

  private void createRow(Sheet sheet, Person person) {
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue(person.getBirthdate());
        ...

If person.getBirthdate() is null we get a NPE:

java.lang.NullPointerException
	at java.util.Calendar.setTime(Calendar.java:1770)
	at org.apache.poi.ss.usermodel.DateUtil.getExcelDate(DateUtil.java:76)
	at org.apache.poi.xssf.usermodel.XSSFCell.setCellValue(XSSFCell.java:742)

My workaround is to check for null and in this case set 
       cell.setCellValue((String) null);

However, in my opinion Cell.setCellValue(Date value) should take care of this, just like Cell.setCellValue(String value)
Comment 1 Dominik Stadler 2016-03-28 20:26:01 UTC
This has been adjusted via r1736923, passing null to setValue() for Date and Calendar leads to a CELL_TYPE_BLANK similar to how String and RichtTextString are handled already.