Reputation: 524
In the following code, I get a java.lang.NullPointerException on the first row.createCell(0)
.
When I remove the spreadSheet.getRow(i+1)
and the if-block after it, then replace them with spreadSheet.createRow(i+1)
it all works.
Sadly doing it that way deletes some existing data in my rows, so I would like to be able to stop this null exception. Can't seem to figure out what is causing it though:
createCell()
and thus doesn't have to be defined beforehandI'm not sure what else to try, I'm mostly perplexed by how it does work when I create a new row but doesn't when I grab an existing one.
//Clone template
System.out.println(workbook.getNameIndex("template"));
spreadSheet = workbook.cloneSheet(workbook.getNameIndex("template")+1, infoString);
//Apply data to cells
for (int i = 0; i < chartData.getItemCount();i++) {
row = spreadSheet.getRow(i+1);
if (row == null) {
spreadSheet.createRow(i+1);
}
row.createCell(0).setCellValue(i+1);
row.createCell(1).setCellValue(chartData.getDataItem(i).getXValue());
row.createCell(2).setCellValue(chartData.getDataItem(i).getYValue());
}
Upvotes: 0
Views: 630
Reputation: 178263
You didn't assign the result of createRow
back to row
. Change
spreadSheet.createRow(i+1);
to
row = spreadSheet.createRow(i+1);
Upvotes: 3