获得空指针异常在Apache的POI创建细胞
问题描述:
我得到一个空指针错误,每次我跑我的code(下图)的,它指出了两个星号指定的行。
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?
答
不幸的是,电池是目前空
不是一个空白单元格。指定纸张上放置数据使得空白。您可能需要先创建单元格,然后检查它是否是一个空白
细胞,而不是空
。
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");
}