从目录中读取多个文件时出现 NullPointerException [英] NullPointerException while reading multiple files from a directory

查看:46
本文介绍了从目录中读取多个文件时出现 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码时,我收到 NullPointerException.此方法用于扫描目录并检索其中的所有文件(.java),然后计算 NLOC 和 NLC.

When I run the following piece of code, I'm getting a NullPointerException. This method is used to scan a directory and retrieve all files(.java) in it and then count the NLOC and NLC.

代码如下:

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):

Here is the error message (I use netbeans as the 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)

推荐答案

folder.listFiles() 返回 null,当文件夹不是目录(在本例中是)或发生IO错误.Web 服务器是否可能没有访问某些文件夹的正确权限?或者他们无法访问(他们通过网络连接).

folder.listFiles() returns null, when folder isn't a directory (which it is in this case) or when an IO error occurs. Is it possible that the web server doesn't have the correct rights to access some folders? Or they are not reachable (they are connected via a network).

folder.listFiles() 添加一个空检查并打印出它返回空的文件夹.这应该可以帮助您确定发生这种情况的原因.如果找不到原因,请在问题中添加发生原因的文件夹.

Add a nullcheck to folder.listFiles() and print out for which folders it returns null. This should help you to determine why it is happening. If you can't find the cause, please add the folders for which it is happening to the question.

这篇关于从目录中读取多个文件时出现 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆