英文:
Exception: Cannot invoke getLastRowNum()" as "sheet" is null
问题
我收到以下异常信息:
>java.lang.RuntimeException: java.lang.NullPointerException: 无法调用"org.apache.poi.xssf.usermodel.XSSFSheet.getLastRowNum()",因为"sheet"为空
以下是要翻译的代码部分:
public static Object[][] getTestDataFromExcel(String sheetName) {
File excelFile = new File("C:\\Users\\abhis\\seleniumHybridFramework\\seleniumHybridFramework\\src\\main\\java\\com\\seleniumHybridFrameworkProject\\qa\\testdata\\seleniumHybridFrameworkProjectTestData.xlsx");
XSSFWorkbook workbook = null;
try {
FileInputStream fisExcel = new FileInputStream(excelFile);
workbook = new XSSFWorkbook(fisExcel);
} catch (Throwable e) {
e.printStackTrace();
}
XSSFSheet sheet = workbook.getSheet(sheetName);
int rows = sheet.getLastRowNum();
int cols = sheet.getRow(0).getLastCellNum();
Object[][] data1 = new Object[rows][cols];
for (int i = 0; i < rows; i++) {
XSSFRow row = sheet.getRow(i + 1);
for (int j = 0; j < cols; j++) {
XSSFCell cell = row.getCell(j);
CellType cellType = cell.getCellType();
switch (cellType) {
case STRING:
data1[i][j] = cell.getStringCellValue();
break;
case NUMERIC:
data1[i][j] = Integer.toString((int) cell.getNumericCellValue());
break;
case BOOLEAN:
data1[i][j] = cell.getBooleanCellValue();
break;
}
}
}
return data1;
}
在XSSFSheet sheet = workbook.getSheet(sheetName)
这一行中,getSheet
被下划线标记,并显示建议,因为它可能会产生Nullpointer Exception
,因为sheet可能为空。
我使用的是poi版本5.2.2、poi-ooxml版本5.2.2、poi-ooxml-schemas版本4.1.0。
我不知道为什么它无法识别工作簿中的工作表,而工作簿为空。
我尝试过File excelFile = new File(System.getProperty("user.dir")+"\\src\\main\\java\\com\\seleniumHybridFrameworkProject\\qa\\testdata\\seleniumHybridFrameworkProjectTestData.xlsx")
。请查看一下。
英文:
I'm getting the following exception:
>java.lang.RuntimeException: java.lang.NullPointerException: Cannot invoke "org.apache.poi.xssf.usermodel.XSSFSheet.getLastRowNum()" as "sheet" is null
public static Object[][] getTestDataFromExcel(String sheetName) {
File excelFile = new File("C:\\Users\\abhis\\seleniumHybridFramework\\seleniumHybridFramework\\src\\main\\java\\com\\seleniumHybridFrameworkProject\\qa\\testdata\\seleniumHybridFrameworkProjectTestData.xlsx");
XSSFWorkbook workbook = null;
try {
FileInputStream fisExcel = new FileInputStream(excelFile);
workbook = new XSSFWorkbook(fisExcel);
} catch (Throwable e) {
e.printStackTrace();
}
XSSFSheet sheet = workbook.getSheet(sheetName);
int rows = sheet.getLastRowNum();
int cols = sheet.getRow(0).getLastCellNum();
Object[][] data1 = new Object[rows][cols];
for (int i = 0; i < rows; i++) {
XSSFRow row = sheet.getRow(i + 1);
for (int j = 0; j < cols; j++) {
XSSFCell cell = row.getCell(j);
CellType cellType = cell.getCellType();
switch (cellType) {
case STRING:
data1[i][j] = cell.getStringCellValue();
break;
case NUMERIC:
data1[i][j] = Integer.toString((int) cell.getNumericCellValue());
break;
case BOOLEAN:
data1[i][j] = cell.getBooleanCellValue();
break;
}
}
}
return data1;
}
Here in XSSFSheet sheet = workbook.getSheet(sheetName)
,
getSheet
is underlined and showing suggestion that this can produce Nullpointer Exception
becoz sheet may be is null
.
I have poi version 5.2.2
poi-ooxml version 5.2.2
poi-ooxml-schemas 4.1.0
I don't know why it is not recognizing the sheet in a sheet path and workbook is null
.
I have tried File excelFile = new File(System.getProperty("user.dir")+"\\src\\main\\java\\com\\seleniumHybridFrameworkProject\\qa\\testdata\\seleniumHybridFrameworkProjectTestData.xlsx")
.
Please look at it.
答案1
得分: 2
错误信息
java.lang.RuntimeException: java.lang.NullPointerException: 无法调用 "org.apache.poi.xssf.usermodel.XSSFSheet.getLastRowNum()",因为 "sheet" 为 null
这说明 sheet
是 null
。
这可能是因为 XSSFWorkbook.getSheet 要么返回提供的名称的 XSSFSheet
,要么如果不存在则返回 null
。因此,在您的代码中,在 XSSFSheet sheet = workbook.getSheet(sheetName);
之后,如果工作簿中没有具有提供的 sheetName
的工作表,sheet
可能为 null。
就像对于 NullPointerException
一样,您必须检查是否得到了一个对象而不是 null
。
...
XSSFSheet sheet = workbook.getSheet(sheetName);
if (sheet != null) {
int rows = sheet.getLastRowNum();
//... 处理工作表的操作
}
...
顺便说一下: poi-ooxml-schemas 4.1.0
不适用于 poi-ooxml
版本 5.2.2
,因为混合使用不支持的 Apache POI 版本会导致意外错误。请参阅 faq-N10204。
请查看 Apache POI - 组件概述。版本 5.2.2
的 poi-ooxml
需要与之相同版本的 poi-ooxml-lite
或 poi-ooxml-full
,即版本 5.2.2
,而不是版本 4.1.0
。
英文:
The error
java.lang.RuntimeException: java.lang.NullPointerException: Cannot invoke "org.apache.poi.xssf.usermodel.XSSFSheet.getLastRowNum()" as "sheet" is null
tells that sheet
is null
.
That's possible because XSSFWorkbook.getSheet either returns the
XSSFSheet
with the name provided or null
if it does not exist. So in your code after XSSFSheet sheet = workbook.getSheet(sheetName);
sheet
might be null if there is not a sheet having name given in sheetName
in the workbook.
As always for NullPointerException
you have to check whether you got an object and not null
.
...
XSSFSheet sheet = workbook.getSheet(sheetName);
if (sheet != null) {
int rows = sheet.getLastRowNum();
//... do what to do with sheet
}
...
Btw.: poi-ooxml-schemas 4.1.0
does not fit to poi-ooxml
version 5.2.2
as mixing Apache POI versions is not supported and will lead to unexpected errors. See faq-N10204.
See Apache POI - Component Overview. The poi-ooxml
of version 5.2.2
needs either poi-ooxml-lite
of same version 5.2.2
or poi-ooxml-full
of same version 5.2.2
but nothing of version 4.1.0
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论