Swing应用程序在ActionEvent上抛出java.lang.NullPointerException

时间:2014-12-19 19:21:55

标签: java swing nullpointerexception

我正在制作一个包含1个TextArea,1个Label和1个TextField的Java Swing应用程序。

我的代码如下:

package mainpack.newboston;

import java.awt.Color;
import java.awt.Container;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Panel extends JPanel implements ActionListener {
    protected JTextArea textarea123;
    protected JTextField textfield1;
    JLabel label;
    JFrame frame;

    public Panel() {
        setLayout(null);

        JTextArea textarea123 = new JTextArea();
        JTextField textfield1 = new JTextField(30);
        JLabel label = new JLabel("Command: ");

        add(textarea123);
        add(textfield1);
        add(label);

        // textarea123.append("Hello");
        textarea123.setEditable(false);
        textarea123.setSize(600, 600);
        textarea123.setBackground(Color.BLACK);
        textarea123.setForeground(Color.WHITE);

        textfield1.setBorder(null);
        textfield1.setBackground(Color.BLACK);
        textfield1.setForeground(Color.white);

        textfield1.addActionListener(this);
        textfield1.setActionCommand("commandexecuted");

        // label.setForeground(Color.WHITE);
        label.setBackground(Color.ORANGE);

        //#######################################################
        Insets insets = getInsets();

        Dimension fieldsize = textfield1.getPreferredSize();
        textfield1.setBounds(63 + insets.left, 555 + insets.top, 537, fieldsize.height);

        textfield1.setFocusable(true);
        textfield1.setEnabled(true);
        Dimension labelsize = label.getPreferredSize();
        label.setBounds(0 + insets.left, 555 + insets.top, labelsize.width, labelsize.height);

        @SuppressWarnings("unused")
        Dimension size = textarea123.getPreferredSize();
        textarea123.setBounds(0 + insets.left, 0 + insets.top, 600, 555);
        //##########################################################
    }

    /**
      * Create the GUI and show it.  For thread safety,
      * this method should be invoked from the
      * event-dispatching thread.
      */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("New Boston");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        frame.add(new Panel());
        //Size and display the window.
        Insets insets = frame.getInsets();
        frame.setResizable(false);
        frame.setSize(600 + insets.left + insets.right, 600 + insets.top + insets.bottom);
        frame.setVisible(true);
    }

    public void ConsoleLog(String message){
        System.out.println(message);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public void textappend(String message) {
        textarea123.append(message);
    }

    public void actionPerformed(ActionEvent evt) {
        if (evt.getActionCommand().equals("commandexecuted")) {
            if (textfield1.getText().equals("Try")) {
                ConsoleLog("Hello");
            }
        }
    }
}

TextField处于焦点时按下enter时出现错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at mainpack.newboston.Panel.actionPerformed(Panel.java:127)
    at javax.swing.JTextField.fireActionPerformed(Unknown Source)
    at javax.swing.JTextField.postActionEvent(Unknown Source)
    at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
    at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    at javax.swing.JComponent.processKeyBinding(Unknown Source)
    at javax.swing.JComponent.processKeyBindings(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(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$400(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)

我试图让它看起来像一个控制台并使用一些命令,但我稍后会这样做,因为我无法使用ActionEvents ...

2 个答案:

答案 0 :(得分:2)

在构造函数中,更改此行

JTextField textfield1 = new JTextField(30);

this.textfield1 = new JTextField(30);

答案 1 :(得分:1)

您正在定义protected JTextField textfield1;等字段。

然后,您可以使用JTextField textfield1 = new JTextField(30);定义局部变量。

因此,例如,textfield1是您的对象(在本地上下文中),this.textfield1为null,因为您从未给它赋值。

我建议为你的字段分配一个变量,如下所示:

textfield1 = new JTextField(30);

在您的字段中尝试此技术,它既可以避免分配同名的局部变量,也可以在分配值时避免编写隐式this

我还建议您查看http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html