PDA

View Full Version : Adding listeners to a JTextField



NeoChaosX
October 27th, 2005, 12:30 AM
Okay, I need some help here. I need to add a listener to a JTextField that gets the data that's in the JTF. However, everytime I run the program, I keep getting NullPointerExceptions when the Listener runs. Here's my code for adding the listener -


JTextField tf = new JTextField("" + d.getValueAt(i), 20);
final JTextField f = tf;
tf.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int value = Integer.getInteger(f.getText());
// remaining code here
. . .
}
});

From the readouts on the code, it's messing up where it's doing the getText() method. So, am I doing this right? Or is there another way to add a Listener that will check for changes in the TextField without blowing up with NullPointerExceptions?

LordHunter317
October 27th, 2005, 05:23 AM
Post more code (pref whole .java file). I think your issue is f isn't declared in a scope where you can actually access it, but because of the brokeness of Java's inner class system, code is being emitted that tries anyway.

NeoChaosX
October 27th, 2005, 06:04 PM
Actually, I figured it out. f is being accessed just fine, it's that I was using the wrong method to get the integer out of the string. What I should've done was Integer.parseInt(), not getInteger() - the latter returns null when trying to read the text in the TextField. Thanks for helping, though.