打开文件会产生java.lang.NullPointerException
我有一个JFrame,在这个框架上有JButton,我想要的是当单击该文件时,用户可以使用javajfilechooser加载一个文件 我像这样声明文件选择器打开文件会产生java.lang.NullPointerException,java,file,jframe,jfilechooser,Java,File,Jframe,Jfilechooser,我有一个JFrame,在这个框架上有JButton,我想要的是当单击该文件时,用户可以使用javajfilechooser加载一个文件 我像这样声明文件选择器 JFileChooser fc; 这是我在动作监听器中为按钮编写的代码 JButton btnLoad = new JButton("Load .txt"); btnLoad.addActionListener(new ActionListener() { public void actionPerformed(
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中出现异常
在maple.Netflix$2.actionPerformed(Netflix.java:73)
在javax.swing.AbstractButton.fireActionPerformed(未知源)
位于javax.swing.AbstractButton$Handler.actionPerformed(未知源)
在javax.swing.DefaultButtonModel.fireActionPerformed(未知源)
位于javax.swing.DefaultButtonModel.setPressed(未知源)
位于javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知源代码)
位于java.awt.AWTEventMulticaster.mouseReleased(未知源)
位于java.awt.Component.ProcessMouseeEvent(未知源)
位于javax.swing.JComponent.ProcessMouseeEvent(未知源)
位于java.awt.Component.processEvent(未知源)
位于java.awt.Container.processEvent(未知源)
位于java.awt.Component.dispatchEventImpl(未知源)
位于java.awt.Container.dispatchEventImpl(未知源)
位于java.awt.Component.dispatchEvent(未知源)
位于java.awt.LightweightDispatcher.RetargetMouseeEvent(未知源)
位于java.awt.LightweightDispatcher.ProcessMouseeEvent(未知源)
位于java.awt.LightweightDispatcher.dispatchEvent(未知源)
位于java.awt.Container.dispatchEventImpl(未知源)
位于java.awt.Window.dispatchEventImpl(未知源)
位于java.awt.Component.dispatchEvent(未知源)
位于java.awt.EventQueue.dispatchEventImpl(未知源)
位于java.awt.EventQueue.access$200(未知源)
在java.awt.EventQueue$3.run处(未知源)
在java.awt.EventQueue$3.run处(未知源)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.security.ProtectionDomain$1.doIntersectionPrivilege(未知源)
位于java.security.ProtectionDomain$1.doIntersectionPrivilege(未知源)
在java.awt.EventQueue$4.run处(未知源)
在java.awt.EventQueue$4.run处(未知源)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.security.ProtectionDomain$1.doIntersectionPrivilege(未知源)
位于java.awt.EventQueue.dispatchEvent(未知源)
位于java.awt.EventDispatchThread.pumpOneEventForFilters(未知源)
位于java.awt.EventDispatchThread.pumpEventsForFilter(未知源)
位于java.awt.EventDispatchThread.pumpEventsForHierarchy(未知源)
位于java.awt.EventDispatchThread.pumpEvents(未知源)
位于java.awt.EventDispatchThread.pumpEvents(未知源)
位于java.awt.EventDispatchThread.run(未知源)
这是第73行
int returnVal = fc.showOpenDialog(Netflix.this);
仅仅声明JFileChooser变量是不够的,因为在使用引用变量fc之前,需要先将其初始化为有效对象。这与任何其他参考变量相同
JFileChooser fc = new JFileChooser();
fc
的值为空。在对其调用方法之前,需要将其设置为适当对象的值