一時的なユーザー名システムを備えた単純なチャット サーバーを構築しています。画面がポップアップすると、ユーザー名を尋ねるシンプルな画面から始まります。何でも入れることができますが、それは純粋に一時的なものです (私も実験中です)。このコードは、プログラムに続行するように指示し、ユーザー名を一時的に使用するためにコードに保存し、メインのチャット サーバー (これまでのところ単なる 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)