我有一个应用程序抛出此错误(仅发生在 xlsx 文件中):
java.lang.NullPointerException
at java.io.File.<init>(File.java:222)
at de.mpicbg.tds.core.ExcelLayout.openWorkbook(ExcelLayout.java:75)
“openWorkbook”方法如下所示:
private void openWorkbook() throws IOException {
File excelFile = new File(fileName);
timestamp = excelFile.lastModified();
// open excel file
if (fileName.endsWith(".xlsx")) {
InputStream excelStream = new BufferedInputStream(new FileInputStream(excelFile));
this.workbook = new XSSFWorkbook((excelStream));
} else {
this.workbook = new HSSFWorkbook(new POIFSFileSystem(new FileInputStream(excelFile)));
}
}
如果我在 Debug模式下执行所有操作,则一切都会顺利并且不会出现错误消息。我对这种行为没有任何解释,也不知道如何解决它。 有人可以帮忙吗?
最佳答案
错误消息显示您的fileName
为null
如果在调试时无法重现此问题,您可以在方法的开头添加一条日志消息。
System.out.println("The fileName is `" + fileName+"`");
我建议您使用参数,而不是使用可能已设置或未设置的字段。
private void openWorkbook(String fileName) throws IOException {
assert fileName != null;
关于java - 调试时空指针(java.io.File)异常消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12859514/