java 使用 Apache POI 的空白列和行的空指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14228887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Null pointer for blank columns and rows using Apache POI
提问by dimas
I need help regarding a recurring problem. Earlier I asked a similar question but it was partially answered. Question hereThe problem is that everytime I wanted to write on a row that doesn't have any data or blank I always get a null pointer error problem. My temporary solution was to populate those rows with data so I can write something on each column. Also the solution given by one of the volunteers who helped me was only a temporary one. I made a few alterations regarding my code (below). Null pointer error points to the line with double asterisk.
我需要有关反复出现的问题的帮助。早些时候我问了一个类似的问题,但它得到了部分回答。问题在这里问题是每次我想在没有任何数据或空白的行上写入时,我总是遇到空指针错误问题。我的临时解决方案是用数据填充这些行,以便我可以在每一列上写一些东西。另外,一位帮助过我的志愿者给出的解决方案也只是暂时的。我对我的代码做了一些改动(如下)。空指针错误指向带有双星号的行。
public void writeSomething(XSSFWorkbook wb){
for (String elem: listOfSheetNames){
if (elem.equals("Sheet2")){
sheet = wb.getSheet(elem);
XSSFRow row = sheet.getRow(2);
**XSSFCell cell = row.getCell(3, Row.CREATE_NULL_AS_BLANK );
if (cell.getCellType() == Cell.CELL_TYPE_BLANK){
cell = row.createCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("this was blank");
}
}
}
}
I went on to research the problem and I saw that they're using MissingCellPolicy indicated hereI went on to try that code but it doesn't work so I checked on Apache Docs and they have Row.Create_Null_as_Blank but am not sure if it is the same with MissingCellPolicy.Create_Null_as_Blank or the latter was just used in previous poi version. Although this should have solved the problem regarding columns or rows which are blank. I still got a null pointer exception error. I am using Apache poi version 3.9 btw if this helps. Any help is greatly appreciate. Thanks
我参加了他们正在使用MissingCellPolicy指示研究这个问题,我看到这里我继续尝试代码,但它不工作,所以我在Apache文档检查,他们有Row.Create_Null_as_Blank,但我不知道这是否是与 MissingCellPolicy.Create_Null_as_Blank 相同,或者后者仅在之前的 poi 版本中使用。尽管这应该解决了有关空白的列或行的问题。我仍然收到空指针异常错误。如果有帮助,我正在使用 Apache poi 3.9 版顺便说一句。非常感谢任何帮助。谢谢
UPDATE 1
更新 1
I updated my code using jims solution. I added a method that will check if a row exists or not. But I still have the same error show with two asterisks. Here is my code:
我使用 jims 解决方案更新了我的代码。我添加了一个方法来检查一行是否存在。但我仍然有相同的错误显示,带有两个星号。这是我的代码:
public void writeSomething(XSSFWorkbook wb){
for (String elem: listOfSheetNames){
if (elem.equals("Sheet2")){
int y=1;
sheet = wb.getSheet(elem);
XSSFRow row = null; //create row variable outside if scope
if (isRowEmpty(sheet.getRow(4))== false){
row = sheet.createRow(4);
}
**XSSFCell cell = row.getCell(y, Row.CREATE_NULL_AS_BLANK );
if (cell.getCellType() == Cell.CELL_TYPE_BLANK){
cell = row.createCell(y);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("Hooooy this was blank");
}
}
}
}
isRowEmpty
isRowEmpty
public boolean isRowEmpty(Row row){
if (row == null){
return true;
}
else {
return false;
}
}
Update 2
更新 2
I found a workaround for my code just in case someone would find it useful. I didn't use the row.getCell since I always encounter NUll pointer error. See code below
我为我的代码找到了一种解决方法,以防万一有人觉得它有用。我没有使用 row.getCell,因为我总是遇到 NULL 指针错误。看下面的代码
public void writeSomething(XSSFWorkbook wb){
for (String elem: listOfSheetNames){
if (elem.equals("Sheet2")){
int y=1; //sets column number
sheet = wb.getSheet(elem);
XSSFRow row = null; //create row variable outside if scope
if (isRowEmpty(sheet.getRow(19))== false){
row = sheet.createRow(19);
XSSFCell cell = row.createCell(y);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("Hooooy this was blank");
}
}
}
}
回答by Jim Garrison
If the cell you need does not exist you must createit with
如果您需要的单元不存在,您必须创建它
HSSFRow.createCell(int columnIndex, int type)
Likewise, if the row does not exist you create it with
同样,如果该行不存在,则使用
HSSFSheet.createRow(int rownum)
XSSF has identical methods as well.
XSSF 也有相同的方法。