Connecting Tech Pros Worldwide Forums | Help | Site Map

getText() causes Exception

Newbie
 
Join Date: Nov 2007
Posts: 16
#1: Jun 14 '08
Hello,

I am writing a simple Java program that has 2 buttons and 1 text field. How
I would like for it to work is that when I click on Button 1, the actionPeformed
method would get the text from the text field and then change the text of
Button 2 to this text (in the text field). And when I click on Button 2, the
actionPeformed method would get the text from the text field and change
the text of Button 1 to this text.

When I run my program the text of the button does not change as intented
and I got error from the screen :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ButtonPanel.actionPerformed(ButtonTest.java:33)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)

My code is as follows :import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ButtonPanel extends JPanel implements ActionListener
{
public ButtonPanel()
{
button1 = new JButton("Button 1");
button2 = new JButton("Button 2");

JTextField chrisField = new JTextField("Hello", 20);

setLayout (new FlowLayout(FlowLayout.LEFT));

add(button1);
add(button2);
add(chrisField);

button1.addActionListener(this);
button2.addActionListener(this);
chrisField.addActionListener(this);
}public void actionPerformed(ActionEvent evt)
{
Object source = evt.getSource();

if (source == button1)
{
/* button2.setText("Hello There"); */
String text = chrisField.getText().trim();
button2.setText(text);
}
else if (source == button2)
{
/* button1.setText("Hello There"); */
String text = chrisField.getText().trim();
button1.setText(text);
}
}

private JButton button1;
private JButton button2;
private JTextField chrisField;
}class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );

Container contentPane = getContentPane();
contentPane.add(new ButtonPanel());
}
}

public class ButtonTest
{
public static void main(String[] args)
{ JFrame frame = new ButtonFrame();frame.show();
}
}

Your help is much appreciated.

Akino.

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Jun 14 '08

re: getText() causes Exception


Have another look at the constructor of your ButtonPanel: you assign that
JTextField to a local variable 'chrisField' that hides your member variable with
the same name. It'll stay null forever.

btw, the 'show()' method of your frame is deprecated, use pack() and setVisible(true)
instead.

kind regards,

Jos
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#3: Jun 14 '08

re: getText() causes Exception


You should take a look at the Sun tutorials:

[http://java.sun.com/docs/books/tutorial/]

Including this GUI HelloWorld:

[http://java.sun.com/docs/books/tutorial/uiswing/examples/start/index.html]


You don't need to use a WindowListener to shut a window and you should learn about invokeLater, too...
Member
 
Join Date: Jul 2007
Location: Seymour,CT , Los Angeles, CA
Posts: 58
#4: Jun 14 '08

re: getText() causes Exception


I also don't think there is a need for an action listener on the text field. No behaviors are triggered by the text field changing, only when a button is clicked.

Tom
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#5: Jun 15 '08

re: getText() causes Exception


Quote:

Originally Posted by tburger

I also don't think there is a need for an action listener on the text field. No behaviors are triggered by the text field changing, only when a button is clicked.

Tom

Then why would JTextField define the method addActionListener? In fact, this is a very useful feature. The listener is notified when enter is pressed whilst focus is on the text field. The text in the text field is passed as the action command of the ActionEvent. Demo:

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class JTextFieldActionListenerExample {
  6.     private ActionListener al = new ActionListener() {
  7.         public void actionPerformed(ActionEvent evt) {
  8.             System.out.println(evt.getActionCommand());
  9.         }
  10.     };
  11.  
  12.     public void go() {
  13.         JTextField text = new JTextField(20);
  14.         text.addActionListener(al);
  15.  
  16.         JFrame f = new JFrame();
  17.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.         f.getContentPane().add(text, BorderLayout.NORTH);
  19.         f.pack();
  20.         f.setLocationRelativeTo(null);
  21.         f.setVisible(true);
  22.     }
  23.  
  24.     public static void main(String[] args) {
  25.         EventQueue.invokeLater(new Runnable() {
  26.             public void run() {
  27.                 new JTextFieldActionListenerExample().go();
  28.             }
  29.         });
  30.     }
  31. }
Familiar Sight
 
Join Date: Mar 2008
Posts: 174
#6: Jun 15 '08

re: getText() causes Exception


You could try approaching the problem a different way and instead of making one event create 2 events for each button. Do something like this for each button:
Expand|Select|Wrap|Line Numbers
  1. button1.addActionListener(new ActionListener()
  2. {
  3.     public void actionPerformed(ActionEvent button1)
  4.     {
  5.         String value = chrisField.getText();
  6.         button2.setText(value);
  7.     }
  8. });
  9.  
Then create another one for button2. I didn't test that little code snippet because I'm lazy but that should just give you an idea of what to do.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#7: Jun 15 '08

re: getText() causes Exception


Quote:

Originally Posted by Kid Programmer

Then create another one for button2. I didn't test that little code snippet because I'm lazy but that should just give you an idea of what to do.

Did you read reply #2? It explains the error; no need to add two listeners because
it doesn't solve the op's problem.

kind regards,

Jos
Familiar Sight
 
Join Date: Mar 2008
Posts: 174
#8: Jun 15 '08

re: getText() causes Exception


Quote:

Originally Posted by JosAH

Did you read reply #2? It explains the error; no need to add two listeners because
it doesn't solve the op's problem.

kind regards,

Jos

I read all the replies. I was just giving him advice. That's how I write my code.
I create a JFrame, I create a Container, a Layout or I use setBounds, I make my components, I add my components to my Container, I set the actions that they preform to whatever I want, then I do all my frame setting things like the title and the default close operation. It always works out for me
Member
 
Join Date: Jul 2007
Location: Seymour,CT , Los Angeles, CA
Posts: 58
#9: Jun 15 '08

re: getText() causes Exception


Quote:

Originally Posted by BigDaddyLH

Then why would JTextField define the method addActionListener? In fact, this is a very useful feature. The listener is notified when enter is pressed whilst focus is on the text field. The text in the text field is passed as the action command of the ActionEvent. Demo:

I understand and agree that this functionality exists and could be used in many situations...

I was merely suggesting that an action listener was not needed in the context of the thread's original code, since all the events are triggered by buttons. I wasn't trying to say that no one should ever use an action listener on a text field...

Good demo on potential functionality, though. Thanks.

Tom
Familiar Sight
 
Join Date: Mar 2008
Posts: 174
#10: Jun 16 '08

re: getText() causes Exception


Quote:

Originally Posted by tburger

I understand and agree that this functionality exists and could be used in many situations...

I was merely suggesting that an action listener was not needed in the context of the thread's original code, since all the events are triggered by buttons. I wasn't trying to say that no one should ever use an action listener on a text field...

Good demo on potential functionality, though. Thanks.

Tom

No problemo. And here is some code that should make the program work:
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public abstract class gui implements ActionListener {
  6.  
  7.     public static void main(String[] args)
  8.     {
  9.         JFrame frame;
  10.         final JButton button1, button2;    
  11.         final JTextField textfield;
  12.         Container contentPane;
  13.         FlowLayout layout;
  14.  
  15.         frame = new JFrame();
  16.  
  17.         contentPane = frame.getContentPane();
  18.  
  19.         layout = new FlowLayout();
  20.  
  21.         frame.setLayout(layout);
  22.  
  23.         button1 = new JButton("Button 1");
  24.         button2 = new JButton("Button 2");
  25.  
  26.         textfield = new JTextField("Type something here");
  27.  
  28.         contentPane.add(button1);
  29.         contentPane.add(button2);
  30.         contentPane.add(textfield);
  31.  
  32.         button1.addActionListener(new ActionListener()
  33.         {
  34.             public void actionPerformed(ActionEvent button1)
  35.             {
  36.                 String value;
  37.                 value = textfield.getText();
  38.                 button2.setText(value);
  39.             }
  40.         });
  41.         button2.addActionListener(new ActionListener()
  42.         {
  43.             public void actionPerformed(ActionEvent button2)
  44.             {
  45.                 String value;
  46.                 value = textfield.getText();
  47.                 button1.setText(value);
  48.             }
  49.         });
  50.  
  51.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52.         frame.pack();
  53.         frame.setVisible(true);
  54.     }
  55. }
  56.  
Reply