문제

The UI has two buttons, browse and submit. I want the user to hit browse to find a file, and then submit to copy it to a different location. However whenever I attempt it, I get this stack trace:

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)

"Submit" button 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);
            }
        }
    }

Browse button code:

@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);
        }

I basically want the variable mod to carry on to the Submit area, but because of where it is set, I am unable to do so.

도움이 되었습니까?

해결책

In your "browse" button code you are initializing a local variable mod like this:

final File mod = fc.getSelectedFile();

In the "Submit" button code you are using a class variable mod. Same name, different variables with different scopes.

Try changing:

final File mod = fc.getSelectedFile();

to

mod = fc.getSelectedFile();

with mod being a private class variable. Also add null check in the "Submit" button code.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top