在 Apache POI 中创建单元格时出现空指针异常 [英] Getting null pointer exception in creating cell in Apache POI
本文介绍了在 Apache POI 中创建单元格时出现空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每次运行我的代码时都会收到空指针错误(如下),它指向用两个星号指定的行.
I am getting a null pointer error everytime I ran my code (below) and it points to the line specified with two asterisk.
public void writeSomething(XSSFWorkbook wb){
for (String elem: listOfSheetNames){
if (elem.equals("Sheet2")){
sheet = wb.getSheet(elem); //sheet is of type XSSFSheet
XSSFRow row = sheet.getRow(0);
**XSSFCell cell = row.getCell(1);
if (cell == null){
cell = row.createCell(1);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("Apple");
}
}
}
}
我是 apache poi 的新手,我只是想将数据写入第二个 Excel 工作表 (Sheet2) 中的空白单元格.我在这里做错了吗?
I am a new to apache poi and am just trying to write data to a blank cell in a 2nd excel sheet (Sheet2). Did I do something wrong here?
推荐答案
不幸的是,该单元格当前为 null
而不是空白单元格.将数据放在指定的工作表上会使其空白.您可能想先创建单元格,然后检查它是否是 blank
单元格而不是 null
.
Unfortunately, the cell is currently null
not a blank cell. Placing data on the specified sheet makes it blank. You might want to create the cell first then check if it is a blank
cell rather than null
.
**XSSFCell cell = row.createCell(1);
if (cell == Cell.CELL_TYPE_BLANK){
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("Apple");
}
这篇关于在 Apache POI 中创建单元格时出现空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文