简体   繁体   中英

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.

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:

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM