如何使用 POI 避免空单元格出现 NullPointerException?
How to avoid NullPointerException with an empty cell using POI?
我正在尝试使用 Apache POI 从 excel.xlsx 文件中获取我的 java 程序中的一些值,但我遇到了麻烦,因为我的循环有时会遇到一个空单元格,然后我得到一个 NullPointerException。我怎样才能 "test" 在阅读单元格之前?这是我的一段代码:
FileInputStream file = new FileInputStream(new File(file));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
int rows;
rows = sheet.getPhysicalNumberOfRows();
for (int i=1;i<rows;i++){
Row row = sheet.getRow(i);
Cell cell = row.getCell(2); // Here is the NullPointerException
String cellString = cell.getStringCellValue();
myArrayList.add(cellString);
}
这让我想到:
java.lang.NullPointerException
at analyse.Analyser.getExcelWords3(Analyser.java:73)
at analyse.Analyser.main(Analyser.java:21)
我想知道是否有可能在尝试读取之前检查单元格是否为空,这样我就不会得到 NPE。提前致谢!
将您的代码包装在 try / catch 语句中,这就是它的用途..
https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
下面一些未经测试的代码可为您提供思路:
for (int i=1;i<rows;i++){
try{
Row row = sheet.getRow(i);
Cell cell = row.getCell(2); // Here is the NullPointerException
String cellString = cell.getStringCellValue();
myArrayList.add(cellString);
}catch(NullPointerException NPE)
{
//what to do when the exception occurs?
}
}
看这个方法:
/**
* Returns the cell at the given (0 based) index, with the specified {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy}
*
* @return the cell at the given (0 based) index
* @throws IllegalArgumentException if cellnum < 0 or the specified MissingCellPolicy is invalid
* @see Row#RETURN_NULL_AND_BLANK
* @see Row#RETURN_BLANK_AS_NULL
* @see Row#CREATE_NULL_AS_BLANK
*/
public XSSFCell getCell(int cellnum, MissingCellPolicy policy) {
应该对你有帮助。
` FileInputStream file = new FileInputStream(new File(file));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
int rows;
String cellString = null;
rows = sheet.getPhysicalNumberOfRows();
for (int i=1;i<rows;i++){
Row row = sheet.getRow(i);
if(row.getCell(2) == null)
{
cellString = null;
}
else
{
Cell cell = row.getCell(2); //if 3rd column of excelsheet then getCell(2)..
cellString = cell.getStringCellValue();
}
myArrayList.add(cellString);
}
if excel cell contain empty/blank then it will be stored as null`
为避免 NullPointerException 添加这个 Row.MissingCellPolicy.CREATE_NULL_AS_BLANK
Cell cell = row.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
这将创建一个空白单元格而不是给你 NPE
我正在尝试使用 Apache POI 从 excel.xlsx 文件中获取我的 java 程序中的一些值,但我遇到了麻烦,因为我的循环有时会遇到一个空单元格,然后我得到一个 NullPointerException。我怎样才能 "test" 在阅读单元格之前?这是我的一段代码:
FileInputStream file = new FileInputStream(new File(file));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
int rows;
rows = sheet.getPhysicalNumberOfRows();
for (int i=1;i<rows;i++){
Row row = sheet.getRow(i);
Cell cell = row.getCell(2); // Here is the NullPointerException
String cellString = cell.getStringCellValue();
myArrayList.add(cellString);
}
这让我想到:
java.lang.NullPointerException
at analyse.Analyser.getExcelWords3(Analyser.java:73)
at analyse.Analyser.main(Analyser.java:21)
我想知道是否有可能在尝试读取之前检查单元格是否为空,这样我就不会得到 NPE。提前致谢!
将您的代码包装在 try / catch 语句中,这就是它的用途.. https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
下面一些未经测试的代码可为您提供思路:
for (int i=1;i<rows;i++){
try{
Row row = sheet.getRow(i);
Cell cell = row.getCell(2); // Here is the NullPointerException
String cellString = cell.getStringCellValue();
myArrayList.add(cellString);
}catch(NullPointerException NPE)
{
//what to do when the exception occurs?
}
}
看这个方法:
/**
* Returns the cell at the given (0 based) index, with the specified {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy}
*
* @return the cell at the given (0 based) index
* @throws IllegalArgumentException if cellnum < 0 or the specified MissingCellPolicy is invalid
* @see Row#RETURN_NULL_AND_BLANK
* @see Row#RETURN_BLANK_AS_NULL
* @see Row#CREATE_NULL_AS_BLANK
*/
public XSSFCell getCell(int cellnum, MissingCellPolicy policy) {
应该对你有帮助。
` FileInputStream file = new FileInputStream(new File(file));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
int rows;
String cellString = null;
rows = sheet.getPhysicalNumberOfRows();
for (int i=1;i<rows;i++){
Row row = sheet.getRow(i);
if(row.getCell(2) == null)
{
cellString = null;
}
else
{
Cell cell = row.getCell(2); //if 3rd column of excelsheet then getCell(2)..
cellString = cell.getStringCellValue();
}
myArrayList.add(cellString);
}
if excel cell contain empty/blank then it will be stored as null`
为避免 NullPointerException 添加这个 Row.MissingCellPolicy.CREATE_NULL_AS_BLANK
Cell cell = row.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
这将创建一个空白单元格而不是给你 NPE