如何选择文件然后在Java中提交?

时间:2014-03-17 15:34:17

标签: java swing stack-trace

UI有两个按钮,浏览和提交。我希望用户点击浏览以查找文件,然后提交以将其复制到其他位置。但是每当我尝试它时,我都会得到这个堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at me.trevor1134.modinjector.ModInjector$3.actionPerformed(ModInjector.java:154)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    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)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    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)
    at java.awt.EventDispatchThread.run(Unknown Source)

"提交"按钮actionPerformed:

    @Override
    public void actionPerformed(ActionEvent arg0) {
        if (mod.exists()) { //line 154 mod is a file object
            OS = System.getProperty("os.name").toLowerCase();
            detectOS();
            modLocation = new File(fullPath);
            if (modLocation.exists() && modLocation.isDirectory()) {
                Path newP = modLocation.toPath();
                Path oldP = mod.toPath();
                try {
                    Files.copy(oldP, newP);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                JOptionPane.showMessageDialog(frame, "Mod successfully copied to: "
                        + fullPath);
                System.out.println("Mod successfully copied to: " + fullPath);
            }
        }
    }

浏览按钮代码:

@Override
        public void actionPerformed(ActionEvent arg0) {
            final JFileChooser fc = new JFileChooser(homePath + "\\Downloads");
            FileNameExtensionFilter filter = new FileNameExtensionFilter("ZIP & JAR Files",
                    "zip", "jar");
            fc.setFileFilter(filter);
            int returnVal = fc.showOpenDialog(frame);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                final File mod = fc.getSelectedFile();

                textField.setText(mod.getAbsolutePath());

                System.out.println("File: " + mod.getName());
            } else {
                System.out.println("Open command cancelled by user.");
            }
            System.out.println(returnVal);
        }

我基本上希望变量mod继续进行提交区域,但由于设置的位置,我无法这样做。

1 个答案:

答案 0 :(得分:3)

在“浏览”按钮代码中,您正在初始化本地变量mod,如下所示:

final File mod = fc.getSelectedFile();

在“提交”按钮代码中,您使用的是类变量mod。相同名称,不同范围的不同变量。

尝试更改:

final File mod = fc.getSelectedFile();

mod = fc.getSelectedFile();

mod是私有类变量。 还要在“提交”按钮代码中添加空检查。