When writing a HSSFWorkbook containing a HSSFSheet with an empty HSSFRow (ie. a row with no cells), a NPE is thrown: The Sample Code: HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(); sheet.createRow(0); workbook.write(new ByteArrayOutputStream()); The StackTrace: java.lang.NullPointerException at org.apache.poi.hssf.model.Sheet.serializeIndexRecord(Sheet.java:787) at org.apache.poi.hssf.model.Sheet.serialize(Sheet.java:731) at org.apache.poi.hssf.usermodel.HSSFWorkbook.getBytes(HSSFWorkbook.java:963) at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:901) at org.apache.poi.hssf.model.MyTestSheet.test(MyTestSheet.java:16) ...
Created attachment 19662 [details] The test case The test case file
Created attachment 19665 [details] A patch proposal for the org.apache.poi.hssf.model.Sheet class I am not sure that 0 for a null cells is a good default value.
Whoops, it seems that with my patch, another NPE occurs whith the same test case, but in RowRecordsAggregate.serialize: The StackTrace java.lang.NullPointerException at org.apache.poi.hssf.record.aggregates.RowRecordsAggregate.serialize(RowRecordsAggregate.java:216) at org.apache.poi.hssf.model.Sheet.serialize(Sheet.java:719) at org.apache.poi.hssf.usermodel.HSSFWorkbook.getBytes(HSSFWorkbook.java:963) at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:901) at org.apache.poi.hssf.model.MyTestSheet.test(MyTestSheet.java:16) ...
Created attachment 19666 [details] A patch proposal for the org.apache.poi.hssf.model.aggregates.RowRecordsAggregate class I just added a check for null value.
Apparently, the binary file generated is "good", ie. I can open it without any error being thrown in Excel.
Julien, patch applied. Thanks a lot for it. Regards, Yegor