0

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!

Nadroihr
  • 17
  • 1
  • 9
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Madhawa Priyashantha Oct 31 '15 at 16:21

1 Answers1

0

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);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • @user5510564, glad it helped. Don't forget to "accept" the answer by clicking on the check mark so people know the problem has been solved. – camickr Oct 31 '15 at 18:27