也許它看起來像一個非常普通的問題,但我也很驚訝這是如何可能的。 我使用下面的語句在我班上的一個創建對象:java對象實例化返回null?
TestVO testVO = new TestVO();
返回null。但是,在將testVO
對象傳遞給方法(解析Excel工作表並將數據設置爲VO)時,它不會拋出NullPointerException
(在eclipse調試模式下,testVO值顯示爲空)。當試圖在下一組語句中使用相同的對象時,它會拋出NPE。
我也很驚訝,爲什麼上面的語句返回null,並沒有分配內存給對象。
請注意在另一個類中創建TestVO
類對象時,它工作正常。這隻發生在一個特定的班級。
下面是類代碼:
public class SheetParser {
public SheetParser() {
}
public void parseSheet(Workbook workbook, List<TestVO> list) {
TestVO testVO = null;
Row row = null;
Cell cell = null;
String header = null;
boolean hasHeaderRow = false;
boolean hasDataRow = false;
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rows = sheet.rowIterator();
List<String> headers = new ArrayList<String>();
try {
while (rows.hasNext()) {
row = rows.next();
if (row.getRowNum() == 0) {
hasHeaderRow = true;
int i = 0;
while (true) {
try {
cell = row.getCell(i++);
if (cell == null) {
break;
}
cell.setCellType(Cell.CELL_TYPE_STRING);
header = cell.getStringCellValue();
if (header == null || "".equals(header.trim())) {
break;
}
headers.add(header.toUpperCase());
} catch (NullPointerException e) {
break;
}
}
validateHeaders(headers);
} else {
hasDataRow = true;
testVO = new TestVO();
// HERE the above statement failed to initialized the object
// and shows the testVO as null
// But below in setRowData method, value is set in testVo
// object after reading from excel sheet
boolean error = setRowData(testVO, headers, row, errorList);
if (!error) {
list.add(testVO);
}
}
}
} catch (Exception e) {
errorList.add(e.getMessage());
}
if (!hasHeaderRow) {
errorList.add("Uploaded Excel file is invalid, headers missing");
}
if (!hasDataRow) {
errorList
.add("Uploaded Excel file is invalid, data row(s) are missing.");
}
}
private boolean setRowData(TestVO testVO, List<String> headersList,
Row row, List<String> errors) {
boolean error = false;
boolean allCellsBlank = true;
List<String> errorList = new ArrayList<String>();
String rowPrefixText = "At Row #";
for (int i = 0; i < headersList.size(); i++) {
String header = headersList.get(i);
String value = "";
try {
row.getCell(i).setCellType(Cell.CELL_TYPE_STRING);
value = row.getCell(i).toString().trim();
} catch (NullPointerException e) {
// do nothing
}
try {
if (header.equalsIgnoreCase("ABC")) {
if (StringUtils.isBlank(value)) {
errorList.add(rowPrefixText + row.getRowNum()
+ ", ABC is required.");
error = true;
} else {
testVO.setAbc(value);
allCellsBlank = false;
}
} else if (header.equalsIgnoreCase("XYZ")) {
if (StringUtils.isBlank(value)) {
errorList.add(rowPrefixText + row.getRowNum()
+ ", XYZ is required.");
error = true;
} else {
testVO.setXyz(value);
allCellsBlank = false;
}
}
} catch (Exception e) {
errorList.add(rowPrefixText + row.getRowNum()
+ ", incorrect data/format in column:" + header);
error = true;
}
}
if (allCellsBlank) {
error = true;
} else if (!errorList.isEmpty()) {
errors.addAll(errorList);
}
return error;
}
private void validateHeaders(List<String> headersList) throws Exception {
if (!headersList.contains("ABC")) {
throw new Exception("Invalid Headers - missing ABC");
} else if (!headersList.contains("XYZ")) {
throw new Exception("Invalid Headers - missing XYZ");
}
}
}
的部分解決方案請加上** **確切代碼 – BobTheBuilder
請查看源代碼加入也不在話下確切位置是問題 – user3129152
難道你還可以添加堆棧跟蹤? –