2013-07-23 84 views
0

當我運行下面的代碼片段時,出現NullPointerException異常。此方法用於掃描目錄並檢索其中的所有文件(.java),然後計算NLOC和NLC。NullPointerException從一個目錄中讀取多個文件

所以這裏是代碼:

public void listFilesForFolder(final File folder) throws IOException { 
    List<String> lines; 
    for (File fileEntry : folder.listFiles()) { // <<--- exception here 
     if (fileEntry.isDirectory()) { 
     listFilesForFolder(fileEntry); 
     } else { 
     if (fileEntry.isFile()) { 
      temp = fileEntry.getName(); 
      if ((temp.substring(temp.lastIndexOf('.') + 1, temp.length()).toLowerCase()).equals("java")) { 
      fileName = folder.getAbsolutePath() + "\\" + fileEntry.getName(); 
      try { 
       lines = getLines(fileName); 
       totalCommentCount += calcComments(lines); 
       totalLOC += calcLOC(lines); 

      } catch (IOException e) { 
       System.out.println("File does not exist!" + e); 
      } 
      } 
     } 
     } 
    } 
    StringBuilder output = new StringBuilder(); 
    output.append("Comment Lines :").append(totalCommentCount); 
    output.append("\n"); 
    output.append("Lines of Code (LOC) :").append(totalLOC); 
    txtDisplayResults.setText(output.toString()); 
} 

以下是錯誤信息(我使用NetBeans作爲IDE):

23-Jul-2013 13:17:53 scaj.GUI listFilesForFolder 
SEVERE: null 
java.lang.NullPointerException 
    at scaj.GUI.listFilesForFolder(GUI.java:61) 
    at scaj.GUI.listFilesForFolder(GUI.java:68) 
    at scaj.GUI.bScanActionPerformed(GUI.java:326) 
    at scaj.GUI.access$100(GUI.java:17) 
    at scaj.GUI$2.actionPerformed(GUI.java:235) 
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) 
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) 
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) 
    at java.awt.Component.processMouseEvent(Component.java:6038) 
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3260) 
    at java.awt.Component.processEvent(Component.java:5803) 
    at java.awt.Container.processEvent(Container.java:2058) 
    at java.awt.Component.dispatchEventImpl(Component.java:4410) 
    at java.awt.Container.dispatchEventImpl(Container.java:2116) 
    at java.awt.Component.dispatchEvent(Component.java:4240) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322) 
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) 
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916) 
    at java.awt.Container.dispatchEventImpl(Container.java:2102) 
    at java.awt.Window.dispatchEventImpl(Window.java:2429) 
    at java.awt.Component.dispatchEvent(Component.java:4240) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121) 
+2

請調試或告訴我們確切的路線! '文件fileEntry:folder.listFiles())''scaj.GUI.listFilesForFolder(GUI.java:61)'... – NINCOMPOOP

+1

'{' 是否引起NullPointerException的行 – wishman

+1

如何聲明'folder'? – araknoid

回答

1

folder.listFiles()返回NULL,當文件夾不是目錄(這是在這種情況下)還是發生IO錯誤時。 Web服務器可能沒有訪問某些文件夾的正確權限嗎?或者他們無法訪問(他們通過網絡連接)。

folder.listFiles()添加一個空檢查並打印出它返回null的文件夾。這應該可以幫助你確定它發生的原因。如果找不到原因,請將發生問題的文件夾添加到問題中。

+0

謝謝,我沒有將null檢查器添加到'folder.listFiles()'中,發現該文件夾造成一切麻煩。 我正在掃描本地硬盤中的'C:\ Users \ UserName \ Documents'文件夾。 以下三個文件夾導致了這些問題。 '1.C:\ Users \用戶名\ Documents \我的音樂 2.C:\ Users \用戶名\ Documents \我的圖片 3.C:\ Users \用戶名\ Documents \我Videos' 任何想法如何跳過這些文件夾(這並不存在)? – wishman

+0

'File [] files = folder.listFiles(); if(files!= null){for(File fileEntry:files){'做到了! 現在就解決了! 謝謝所有:) – wishman

1

您的folder.listFiles()返回null。該listFiles()方法:

Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

相關問題