将文件夹中的文件列出到 JOptionPane
List files in a folder to a JOptionPane
最近我一直在开发一个程序来列出文件夹中的文件,以便它们显示在 JOptionPane
。
我试图通过在文件夹对象上调用 File.listFiles
来获得 File[]
,但它在行 for (int i = 0; i < listOfFiles.length; i++) {
上产生了 NullPointerException
。
我的代码是这样的:
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.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.access0(EventQueue.java:103)
at java.awt.EventQueue.run(EventQueue.java:694)
at java.awt.EventQueue.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue.run(EventQueue.java:708)
at java.awt.EventQueue.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
试试这个:
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);
}
}
});
}
您的初始代码中的错误:
- 您在 for 循环中声明了 StringBuffer。在每个循环中构造一个新对象。
- 不需要在每个循环上执行 sb.toString()。通常只做一次。
- File.listFiles() 可能 return
null
用于无效路径。所以 listOfFiles
的值应该被评估为 null
.
需要改进的地方:
"invalid path" 和 "empty directory" 的不同消息。现在都一样,但不应该这样。
尾随逗号!对于包含一些文件的有效目录,输出将是:"a, b, c,"。最后一个逗号是不必要的。您必须以某种方式避免或删除它,这是一个单独的(也是一个非常受欢迎的)问题。祝你好运。 )
您能否将行号映射到代码中出现异常的行?它可能与 JOptionPane 无关。
我怀疑 NPE 可能会出现在这条线上:
for (int i = 0; i < listOfFiles.length; i++) {
列出无效文件夹时。 File.listFiles() 可以 return 为空。
您需要将 JFrame 对象传递给该方法。
JOptionPane.showMessageDialog(urJFrameObject, s);
如果具有 msgBox( ) 函数的 class 扩展了 JFrame,则更改代码如下:
JOptionPane.showMessageDialog(this, s);
最近我一直在开发一个程序来列出文件夹中的文件,以便它们显示在 JOptionPane
。
我试图通过在文件夹对象上调用 File.listFiles
来获得 File[]
,但它在行 for (int i = 0; i < listOfFiles.length; i++) {
上产生了 NullPointerException
。
我的代码是这样的:
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.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.access0(EventQueue.java:103)
at java.awt.EventQueue.run(EventQueue.java:694)
at java.awt.EventQueue.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue.run(EventQueue.java:708)
at java.awt.EventQueue.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
试试这个:
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);
}
}
});
}
您的初始代码中的错误:
- 您在 for 循环中声明了 StringBuffer。在每个循环中构造一个新对象。
- 不需要在每个循环上执行 sb.toString()。通常只做一次。
- File.listFiles() 可能 return
null
用于无效路径。所以listOfFiles
的值应该被评估为null
.
需要改进的地方:
"invalid path" 和 "empty directory" 的不同消息。现在都一样,但不应该这样。
尾随逗号!对于包含一些文件的有效目录,输出将是:"a, b, c,"。最后一个逗号是不必要的。您必须以某种方式避免或删除它,这是一个单独的(也是一个非常受欢迎的)问题。祝你好运。 )
您能否将行号映射到代码中出现异常的行?它可能与 JOptionPane 无关。
我怀疑 NPE 可能会出现在这条线上:
for (int i = 0; i < listOfFiles.length; i++) {
列出无效文件夹时。 File.listFiles() 可以 return 为空。
您需要将 JFrame 对象传递给该方法。
JOptionPane.showMessageDialog(urJFrameObject, s);
如果具有 msgBox( ) 函数的 class 扩展了 JFrame,则更改代码如下:
JOptionPane.showMessageDialog(this, s);