2012-10-12 28 views
1

我有(只發生與XLSX文件)會拋出這個錯誤的應用程序:空指針(java.io.File的)異常,而調試dissappears

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))); 
    } 
} 

如果我在調試模式下執行所有操作,一切都會順利進行,並且不會出現錯誤消息。我沒有任何解釋這種行爲,也沒有任何想法如何解決這個問題。 任何人都可以幫忙嗎?

+0

什麼版本Java?您可能需要查看'File.java'源文件。 – noahlz

+0

你有'null''fileName'。 –

+0

'fileName'在哪裏初始化? –

回答

2

該錯誤消息說,你fileNamenull

如果調試你可以在你的方法開始添加日誌消息,當您無法重現此。

System.out.println("The fileName is `" + fileName+"`"); 

而不是使用可能或可能不會設置的字段,我建議您使用一個參數。

private void openWorkbook(String fileName) throws IOException { 
    assert fileName != null; 
+0

你可能是對的。文件名應該與GUI組件中的ChangeEvent一起提供。出於某種原因,我認爲該組件提供null ... – July