我有一个JFrame,在框架上我有JButton,我想要的是当单击该文件时用户可以使用java JFileChooser加载文件。
我声明FileChooser是这样的。
JFileChooser fc;
然后这是我在按钮的动作监听器中的代码。
JButton btnLoad = new JButton("Load .txt");
btnLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(OpenFile.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
System.out.println("Opening: " + file.getName() + ".");
} else {
System.out.println("Open command cancelled by user.");
}
}
});
它产生的错误是
线程中的异常" AWT-EventQueue-0"显示java.lang.NullPointerException at maple.Netflix $ 2.actionPerformed(Netflix.java:73) 在javax.swing.AbstractButton.fireActionPerformed(未知来源) at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source) 在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源) 在javax.swing.DefaultButtonModel.setPressed(未知来源) 在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源) 在java.awt.AWTEventMulticaster.mouseReleased(未知来源) at java.awt.Component.processMouseEvent(Unknown Source) 在javax.swing.JComponent.processMouseEvent(未知来源) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 在java.awt.EventQueue.access $ 200(未知来源) 在java.awt.EventQueue $ 3.run(未知来源) 在java.awt.EventQueue $ 3.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) 在java.awt.EventQueue $ 4.run(未知来源) 在java.awt.EventQueue $ 4.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 在java.awt.EventDispatchThread.run(未知来源)
这是第73行。
int returnVal = fc.showOpenDialog(Netflix.this);
答案 0 :(得分:5)
仅仅声明JFileChooser变量是不够的,因为您需要在使用它之前首先将引用变量fc初始化为有效对象。这与任何其他参考变量相同。
JFileChooser fc = new JFileChooser();
答案 1 :(得分:2)
fc
的值为null。在调用方法之前,需要将其设置为适当对象的值。