[英]Exception in thread “AWT-EventQueue-0” (Java)
i'm trying to build a simple chatting plataform, but am having a problem when i try to get the text from the JTextField to place it in the JTextArea. 我正在尝试构建一个简单的聊天平台,但是当我尝试从JTextField获取文本以将其放置在JTextArea中时遇到了问题。
readTextBox.setRows(37);
readTextBox.setColumns(45);
JTextArea readTextBox = new JTextArea();
JTextField typeTextBox = new JTextField(30);
JButton send = new JButton("Send");
send.addActionListener(new sendMessageListener());
public class sendMessageListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String message = typeTextBox.getText();
System.out.println(message);
readTextBox.setText(readTextBox.getText()+"\n"+message);
}
}
This is the base of the code i'm using. 这是我正在使用的代码的基础。 The problem is, when i type on the JTextField and press the "Send" button, i get this error:
问题是,当我在JTextField上键入并按“发送”按钮时,出现此错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at pt.iscte.pcd.QuequeApp.Janela$sendMessageListener.actionPerformed(Janela.java:97)
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$500(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)
I know the error is a nullpointerexception but i can't seem to find why, that Syso i used there isn't even printing the text, so i guess the problem is getting the text from the JTextField, but i've been typing there, so it's not null. 我知道错误是一个nullpointerexception,但我似乎找不到原因,我使用的那个Syso甚至没有打印文本,所以我猜问题是从JTextField获取文本,但是我一直在那儿键入,因此它不是null。
Can someone please help me? 有人可以帮帮我吗?
Thanks! 谢谢!
Because you have defined your variables twice, one as an instance variable and once as a local variable. 因为您已经定义了两次变量,所以一次定义为实例变量,一次定义为局部变量。 Get rid or the local variable:
摆脱掉或局部变量:
//JTextArea readTextBox = new JTextArea();
//JTextField typeTextBox = new JTextField(30);
readTextBox = new JTextArea();
typeTextBox = new JTextField(30);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.