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

Swing应用程序在ActionEvent上抛出java.lang.NullPointerException,java,swing,nullpointerexception,Java,Swing,Nullpointerexception,我正在制作一个JavaSwing应用程序,它包含1个TextArea、1个Label和1个TextField 我的代码如下: 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.

我正在制作一个JavaSwing应用程序,它包含1个TextArea、1个Label和1个TextField

我的代码如下:

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");
            }
        }
    }
}
文本字段处于焦点时按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…

在构造函数中,更改这一行

JTextField textfield1 = new JTextField(30);


在构造函数中,更改此行

JTextField textfield1 = new JTextField(30);


在构造函数中,更改此行

JTextField textfield1 = new JTextField(30);


在构造函数中,更改此行

JTextField textfield1 = new JTextField(30);


您正在定义字段,例如
protectedjtextfield textfield1

然后定义局部变量,例如使用
JTextField textfield1=newjtextfield(30)

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

我建议为字段指定一个变量,如下所示:

textfield1=新的JTextField(30)

在字段中尝试这种技术,它既可以避免指定同名的局部变量,也可以避免在指定值时编写隐式
this


我还建议看一下

您正在定义的字段,如
受保护的JTextField textfield1

然后定义局部变量,例如使用
JTextField textfield1=newjtextfield(30)

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

我建议为字段指定一个变量,如下所示:

textfield1=新的JTextField(30)

在字段中尝试这种技术,它既可以避免指定同名的局部变量,也可以避免在指定值时编写隐式
this


我还建议看一下

您正在定义的字段,如
受保护的JTextField textfield1

然后定义局部变量,例如使用
JTextField textfield1=newjtextfield(30)

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

我建议为字段指定一个变量,如下所示:

textfield1=新的JTextField(30)

在字段中尝试这种技术,它既可以避免指定同名的局部变量,也可以避免在指定值时编写隐式
this


我还建议看一下

您正在定义的字段,如
受保护的JTextField textfield1

然后定义局部变量,例如使用
JTextField textfield1=newjtextfield(30)

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

我建议为字段指定一个变量,如下所示:

textfield1=新的JTextField(30)

在字段中尝试这种技术,它既可以避免指定同名的局部变量,也可以避免在指定值时编写隐式
this


我还建议看一看

真的,这么简单?我想在几个小时内解决这个问题。。。非常感谢:))对你的文本区域和标签也这样做。真的,这么简单吗?我想在几个小时内解决这个问题。。。非常感谢:))对你的文本区域和标签也这样做。真的,这么简单吗?我想在几个小时内解决这个问题。。。非常感谢:))对你的文本区域和标签也这样做。真的,这么简单吗?我想在几个小时内解决这个问题。。。非常感谢:))请对您的文本区域和标签执行相同的操作。