声纳为下面的代码行提供空指针违规。
cell1.getRow()
请帮助我们解决此问题。
private List<InvalidUploadedExcelData> validateSheet(Sheet sheet) throws Exception {
InvalidUploadedExcelData ITD;
ArrayList<InvalidUploadedExcelData> returnedInvalidTestDataList = new ArrayList<InvalidUploadedExcelData>();
Cell cell1, cell2, cell3;
for (int i = 1; i < sheet.getRows(); i++) {
cell1 = sheet.getCell(0, i);
cell2 = sheet.getCell(1, i);
cell3 = sheet.getCell(2, i);
if ((cell1 == null || StringUtils.isEmpty(cell1.getContents().trim()))) {
ITD = new InvalidUploadedExcelData(TITLE_TEST_ID, "Row-" + (cell1.getRow() + 1) + " Column-" + (cell1.getColumn() + 1) + " is missing");
returnedInvalidTestDataList.add(ITD);
} else if (!isValidProperty(cell1.getContents().trim())) {
ITD = new InvalidUploadedExcelData(TITLE_TEST_ID + ":" + cell1.getContents().trim(), "Row-" + (cell1.getRow() + 1) + " Column-" + (cell1.getColumn() + 1) + " is not valid");
returnedInvalidTestDataList.add(ITD);
}
}
return returnedInvalidTestDataList;
}
答案 0 :(得分:0)
如果在if条件中检查null时,如果cell1实际为null,则以下行将导致NullPointerException
,但您仍然进入分支,然后访问那里的cell1.getRow()
。
if ((cell1 == null || StringUtils.isEmpty(cell1.getContents().trim()))) {
ITD = new InvalidUploadedExcelData(TITLE_TEST_ID, "Row-" + (cell1.getRow() + 1) + " Column-" + (cell1.getColumn() + 1) + " is missing");
在这种情况下,您需要以不同方式获取行/列信息才能安全。