[英]Java Apache-POI nullpointer exception on creating a cell in an existing row
In the following code, I get a java.lang.NullPointerException on the first row.createCell(0)
. 在以下代码中,我在第一
row.createCell(0)
上获得了java.lang.NullPointerException.createCell 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. 当我删除
spreadSheet.getRow(i+1)
及其后的if块,然后将其替换为spreadSheet.createRow(i+1)
它们都可以工作。
Sadly doing it that way deletes some existing data in my rows, so I would like to be able to stop this null exception. 遗憾的是,这样做会删除行中的一些现有数据,因此我希望能够停止此null异常。 Can't seem to figure out what is causing it though:
似乎无法弄清楚是什么原因造成的:
createCell()
and thus doesn't have to be defined beforehand createCell()
新创建的,因此不必事先定义 I'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());
}
You didn't assign the result of createRow
back to row
. 您没有将
createRow
的结果分配回row
。 Change 更改
spreadSheet.createRow(i+1);
to 至
row = spreadSheet.createRow(i+1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.