0

我正在构建一个带有临时用户名系统的简单聊天服务器。当屏幕弹出时,它以一个简单的屏幕开始,询问您的用户名。你可以放任何你想要的东西,这纯粹是暂时的(我也在试验)。代码告诉程序继续,将用户名保存到代码中以供临时使用,然后进入主聊天服务器(目前只是一个 GUI)。但事实并非如此。相反,Java 非常粗鲁地告诉我我犯了一个错误,我是一个坏人。我不确定我做错了什么或它是如何发生的,因为一切似乎都很好,我认为我的按钮监听器是正确的。我的错误是什么,我该如何解决?这是我的代码:

package coltGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class MainGUI {

    String username;

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        MainGUI gui = new MainGUI();
        gui.preDisplay();
    }

    JFrame frame;
    JButton sendMessage;
    JTextField messageBox;
    JTextArea chatBox;
    JTextArea usernameChooser;
    JFrame preFrame;

    private void preDisplay() {
        JFrame preFrame = new JFrame("Choose your username!(Colt chat v 0.1");
        JTextField usernameChooser = new JTextField();
        JLabel chooseUsernameLabel = new JLabel("Pick a username:");
        JButton enterServer = new JButton("Enter Chat Server");
        JPanel prePanel = new JPanel(new GridBagLayout());
        GridBagConstraints preRight = new GridBagConstraints();
        preRight.anchor = GridBagConstraints.EAST;
        GridBagConstraints preLeft = new GridBagConstraints();
        preLeft.anchor = GridBagConstraints.WEST;
        preRight.weightx = 2.0;
        preRight.fill = GridBagConstraints.HORIZONTAL;
        preRight.gridwidth = GridBagConstraints.REMAINDER;
        preFrame.add(BorderLayout.CENTER, prePanel);
        prePanel.add(chooseUsernameLabel, preLeft);
        prePanel.add(usernameChooser, preRight);
        preFrame.add(BorderLayout.SOUTH, enterServer);
        preFrame.setVisible(true);
        preFrame.setSize(300, 300);

        enterServer.addActionListener(new enterServerButtonListener());
    }

    public void display() {

        frame = new JFrame("Colt Chat (Alpha 0.1)");
        JPanel southPanel = new JPanel();

        frame.add(BorderLayout.SOUTH, southPanel);
        southPanel.setBackground(Color.BLUE);
        southPanel.setLayout(new GridBagLayout());

        messageBox = new JTextField(30);
        sendMessage = new JButton("Send Message");
        chatBox = new JTextArea();
        chatBox.setEditable(false);
        frame.add(new JScrollPane(chatBox), BorderLayout.CENTER);

        chatBox.setLineWrap(true);

        GridBagConstraints left = new GridBagConstraints();
        left.anchor = GridBagConstraints.WEST;
        GridBagConstraints right = new GridBagConstraints();
        right.anchor = GridBagConstraints.EAST;
        right.weightx = 2.0;

        southPanel.add(messageBox, left);
        southPanel.add(sendMessage, right);

        chatBox.setFont(new Font("Serif", Font.PLAIN, 15));
        sendMessage.addActionListener(new sendMessageButtonListener());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(470, 300);
    }

    class sendMessageButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            if (messageBox.getText().length() < 1) {
                // do nothing 
            } else if (messageBox.getText().equals(".clear")) {
                chatBox.setText("Cleared all messages\n");
                messageBox.setText("");
            } else {
                chatBox.append("<" + username + ">:" + messageBox.getText() + "\n");
                messageBox.setText("");
            }
        }
    }

    class enterServerButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            username = usernameChooser.getText();
            preFrame.setVisible(false);
            frame.setVisible(true);

        }

    }
}

这就是那个错误。当我尝试按下选择用户名 GUI 上的“输入服务器”按钮时会发生这种情况:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at coltGUI.MainGUI$enterServerButtonListener.actionPerformed(MainGUI.java:113)
    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$000(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)
4

1 回答 1

4

因为您的侦听器所依赖的字段尚未初始化

JFrame frame;
JButton sendMessage;
JTextField messageBox;
JTextArea chatBox;
JTextArea usernameChooser;
JFrame preFrame;

private void preDisplay() {
    // All the above fields have been redeclared here and are local to the constrcutor
    JFrame preFrame = new JFrame("Choose your username!(Colt chat v 0.1");
    JTextField usernameChooser = new JTextField();
    JLabel chooseUsernameLabel = new JLabel("Pick a username:");
    JButton enterServer = new JButton("Enter Chat Server");
    JPanel prePanel = new JPanel(new GridBagLayout());
    GridBagConstraints preRight = new GridBagConstraints();
    preRight.anchor = GridBagConstraints.EAST;
    GridBagConstraints preLeft = new GridBagConstraints();

应该更像...

JFrame frame;
JButton sendMessage;
JTextField messageBox;
JTextArea chatBox;
JTextArea usernameChooser;
JFrame preFrame;

private void preDisplay() {
    // All the above fields have been redeclared here and are local to the constrcutor
    preFrame = new JFrame("Choose your username!(Colt chat v 0.1");
    usernameChooser = new JTextField();
    JLabel chooseUsernameLabel = new JLabel("Pick a username:");
    JButton enterServer = new JButton("Enter Chat Server");
    JPanel prePanel = new JPanel(new GridBagLayout());
于 2013-04-24T03:05:20.370 回答