getText() causes Exception | Newbie | | Join Date: Nov 2007
Posts: 16
| | |
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.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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
|  | Moderator | | Join Date: Dec 2007 Location: Kelowna, BC Canada
Posts: 1,212
| | | 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
| | | 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
|  | Moderator | | Join Date: Dec 2007 Location: Kelowna, BC Canada
Posts: 1,212
| | | 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: - import java.awt.*;
-
import java.awt.event.*;
-
import javax.swing.*;
-
-
public class JTextFieldActionListenerExample {
-
private ActionListener al = new ActionListener() {
-
public void actionPerformed(ActionEvent evt) {
-
System.out.println(evt.getActionCommand());
-
}
-
};
-
-
public void go() {
-
JTextField text = new JTextField(20);
-
text.addActionListener(al);
-
-
JFrame f = new JFrame();
-
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
f.getContentPane().add(text, BorderLayout.NORTH);
-
f.pack();
-
f.setLocationRelativeTo(null);
-
f.setVisible(true);
-
}
-
-
public static void main(String[] args) {
-
EventQueue.invokeLater(new Runnable() {
-
public void run() {
-
new JTextFieldActionListenerExample().go();
-
}
-
});
-
}
-
}
| | Familiar Sight | | Join Date: Mar 2008
Posts: 174
| | | 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: -
button1.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent button1)
-
{
-
String value = chrisField.getText();
-
button2.setText(value);
-
}
-
});
-
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.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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
| | | 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
| | | 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
| | | 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: -
import javax.swing.*;
-
import java.awt.*;
-
import java.awt.event.*;
-
-
public abstract class gui implements ActionListener {
-
-
public static void main(String[] args)
-
{
-
JFrame frame;
-
final JButton button1, button2;
-
final JTextField textfield;
-
Container contentPane;
-
FlowLayout layout;
-
-
frame = new JFrame();
-
-
contentPane = frame.getContentPane();
-
-
layout = new FlowLayout();
-
-
frame.setLayout(layout);
-
-
button1 = new JButton("Button 1");
-
button2 = new JButton("Button 2");
-
-
textfield = new JTextField("Type something here");
-
-
contentPane.add(button1);
-
contentPane.add(button2);
-
contentPane.add(textfield);
-
-
button1.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent button1)
-
{
-
String value;
-
value = textfield.getText();
-
button2.setText(value);
-
}
-
});
-
button2.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent button2)
-
{
-
String value;
-
value = textfield.getText();
-
button1.setText(value);
-
}
-
});
-
-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
frame.pack();
-
frame.setVisible(true);
-
}
-
}
-
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|