当actionlistener是calle时,我得到"线程中的异常" AWT-EventQueue-0"显示java.lang.NullPointerException"

时间:2015-04-22 06:01:39

标签: java windows swingbuilder

运行以下代码:

import javax.swing.*;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;

import matlabcontrol.MatlabConnectionException;
import matlabcontrol.MatlabInvocationException;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.RowSpec;

class BackgroundImageJFrame1 extends JFrame {
    public BackgroundImageJFrame1() {
        JLabel l1_1;
        JFileChooser fc = null;
        final JTextField textField;
        final JButton jb1 = null;

        // getContentPane().setLayout(new BorderLayout());
        // setContentPane(new JLabel(new ImageIcon("Car-1202184041.png")));
        // getContentPane().setLayout(new FlowLayout());

        JFrame Frame1 = new JFrame();
        // Just for refresh :) Not optional!
        Frame1.setSize(399, 399);
        setSize(400, 400);

        Frame1.setTitle("Parallel Session");
        Frame1.setSize(400, 400);
        Frame1.setLocationRelativeTo(null);
        Frame1.setDefaultCloseOperation(EXIT_ON_CLOSE);
        Frame1.setVisible(true);

        final JPanel panel1 = new JPanel();
        panel1.setLayout(new FormLayout(new ColumnSpec[] {
                ColumnSpec.decode("132px"),
                FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
                ColumnSpec.decode("63px"), FormFactory.UNRELATED_GAP_COLSPEC,
                ColumnSpec.decode("28px"),
                FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
                ColumnSpec.decode("91px"), }, new RowSpec[] {
                RowSpec.decode("23px"), FormFactory.LINE_GAP_ROWSPEC,
                RowSpec.decode("20px"), FormFactory.LINE_GAP_ROWSPEC,
                RowSpec.decode("23px"), FormFactory.LINE_GAP_ROWSPEC,
                RowSpec.decode("28px"), RowSpec.decode("23px"),
                FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
                RowSpec.decode("22px"), RowSpec.decode("14px"), }));

        l1_1 = new JLabel("Parallel Simulation Model");
        panel1.add(l1_1, "1, 1, 7, 1, center, top");
        l1_1.setForeground(Color.RED);
        l1_1.setFont(new Font("Palatino Linotype", Font.BOLD, 16));
        l1_1.setToolTipText("Getting Started");
        l1_1.setBounds(86, 54, 219, 45);

        // Icon image = new ImageIcon("Car-1202184041.png");
        // panel1.add((Component) image);

        JLabel j1 = new JLabel("Select your model");
        panel1.add(j1, "1, 3, right, center");
        JTextField jt1 = new JTextField();

        jt1.setPreferredSize(new Dimension(160, 20));
        panel1.add(jt1, "3, 3, 5, 1, left, top");
        // System.out.println("boom");
        JButton jb11 = new JButton("Browse");
        jb11.setPreferredSize(new Dimension(100, 20));
        panel1.add(jb11, "3, 5, 3, 1, right, center");

        jb11.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseActionPerformed(null);
            }

        });
        JButton jb2 = new JButton("OK");
        panel1.add(jb2, "7, 5, left, top");

        jb2.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent arg0) {

                try {
                    multi m = new multi();
                } catch (InvocationTargetException | InterruptedException
                        | MatlabConnectionException | MatlabInvocationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        });
        // System.out.println("boom");
        JLabel label1 = new JLabel("Enter the number of instances");
        panel1.add(label1, "1, 7, 1, 2, left, center");
        // System.out.println("boom");
        Frame1.setVisible(true);
        Frame1.setSize(300, 300);
        // System.out.println("boom");
        Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Frame1.getContentPane().add(panel1);
        panel1.setBackground(Color.gray);

        final JSpinner spinner = new JSpinner();
        panel1.add(spinner, "3, 7, 3, 1");

        JButton b11 = new JButton("SET");
        panel1.add(b11, "7, 7, left, top");

        JProgressBar progressBar = new JProgressBar();
        progressBar.setValue(1);
        progressBar.setIndeterminate(true);
        panel1.add(progressBar, "3, 10, 4, 1");

        // done

        b11.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String a = (String) spinner.getValue();
                System.out.println(" " + a);
                try {
                    int itr = Integer.parseInt(a);
                    for (int i = 1; i <= itr; i++) {
                        multi m = new multi();
                        System.out.println("qweqwe");
                        JLabel l1 = new JLabel("MANUEVER DETAILS");
                        JTextField p = new JTextField("Enter the manuever here");
                        panel1.add(l1);
                        panel1.add(p);
                    }
                } catch (InvocationTargetException | InterruptedException
                        | MatlabConnectionException | MatlabInvocationException e) {
                    // TODO Auto-generated catch block
                    ((Throwable) e).printStackTrace();
                }

            }
        });

    }

    private void add(ImageIcon image) {
        // TODO Auto-generated method stub

    }

    private void btnBrowseActionPerformed(ActionListener evt) {

        Object fc = null;
        if (fc == null) {
            fc = new JFileChooser();
        }

        // Show it.
        int returnVal = ((JFileChooser) fc).showOpenDialog(null);

        AbstractButton textField = null;
        // Process the results.
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            textField.setText(((JFileChooser) fc).getSelectedFile().getPath());
        } else {
            textField.setText("");
        }

        // Reset the file chooser for the next time it's shown.
        ((JFileChooser) fc).setSelectedFile(null);

    }

    public static void main(String[] args) throws MatlabConnectionException,
            MatlabInvocationException, IOException {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    System.out.println("hey");

                    new BackgroundImageJFrame1();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });

    }
}

我得到以下结果:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at BackgroundImageJFrame1.btnBrowseActionPerformed(BackgroundImageJFrame1.java:180)
    at BackgroundImageJFrame1.access$0(BackgroundImageJFrame1.java:165)
    at BackgroundImageJFrame1$1.actionPerformed(BackgroundImageJFrame1.java:90)
    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$300(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)

2 个答案:

答案 0 :(得分:2)

因为以下原因:

    AbstractButton textField = null;
    // Process the results.
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        textField.setText(((JFileChooser) fc).getSelectedFile().getPath());
    } else {
        textField.setText("");
    }

您正尝试在null对象引用中设置文本。你可以使用类似的东西:

AbstractButton textField = new JButton();// or any other class object which you want

答案 1 :(得分:2)

您将文本字段设置为null:

    AbstractButton textField = null;
    // Process the results.
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        textField.setText(((JFileChooser) fc).getSelectedFile().getPath());
    } else {
        textField.setText("");
    }

然后你拨打setText(),这将导致NPE。

所以我认为你必须将'final JTextField textField;'更改为类变量并删除行AbstractButton textField = null;