将文件夹中的文件列出到JOptionPane

时间:2015-06-06 18:18:01

标签: java nullpointerexception java-io

最近我一直在制作一个程序来列出文件夹中的文件,以便它们显示在JOptionPane上。

我试图通过调用文件夹对象上的File[]来获取File.listFiles,但会在NullPointerException行上生成for (int i = 0; i < listOfFiles.length; i++) {

我的代码是:

if (OS.contains("Mac")){
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                File folder = new File("/Users/"+ user +"/Desktop");

                File[] listOfFiles = folder.listFiles();
                String herd = "";
                for (int i = 0; i < listOfFiles.length; i++) {
                      StringBuffer sb = new StringBuffer(2000);
                      String s = listOfFiles[i].getName();
                      herd = sb.append(s).append(", ").toString();
                }

                msgbox(herd);

            }
        });
    }

msgbox是:

public void msgbox(String s){
    JOptionPane.showMessageDialog(null, s);
}

NullPointerException堆栈跟踪如下:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at goatSoftware.CreateJFrame$1.actionPerformed(CreateJFrame.java:93)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6516)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6281)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

3 个答案:

答案 0 :(得分:1)

试试这个:

if(OS.contains("Mac"))

{
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String path = "/Users/" + user + "/Desktop";
            File folder = new File(path);

            File[] listOfFiles = folder.listFiles();
            if (listOfFiles != null && listOfFiles.length > 0) {
                StringBuffer sb = new StringBuffer(2000);
                for (int i = 0; i < listOfFiles.length; i++) {
                    String s = listOfFiles[i].getName();
                    sb.append(s).append(", ");
                }
                msgbox(sb.toString());

            } else {
                msgbox("No files in the folder " + path);
            }
        }
    }); 
}

初始代码中的错误:

  1. 您在for循环中声明了StringBuffer。在每个循环上构建一个新对象。
  2. 不需要在每个循环上执行sb.toString()。它通常只做一次。
  3. File.listFiles()可能会返回null作为无效路径。因此,应listOfFiles评估null的值。
  4. 需要改进的地方:

    1. “无效路径”和“空目录”的不同消息。现在它们都是一样的,但它不应该是。

    2. 尾随逗号!对于包含某些文件的有效目录,输出将为:“a,b,c,”。最后一个逗号是不必要的。你必须以某种方式避免或删除它,这是一个单独的(和一个非常受欢迎的)问题。祝它好运。 )

答案 1 :(得分:1)

您可以将行号映射到代码中的行,其中会出现异常吗?它可能与JOptionPane无关。

我怀疑NPE可能会出现在这条线上:

for (int i = 0; i < listOfFiles.length; i++) {

列出无效文件夹时。 File.listFiles()可以返回null。

答案 2 :(得分:0)

您需要将JFrame对象传递给方法。

JOptionPane.showMessageDialog(urJFrameObject, s);

如果具有msgBox()函数的类扩展了JFrame,则更改代码如下:

JOptionPane.showMessageDialog(this, s);