Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField.setText Not Working

 
Don Gardner
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having some problems setting the value of a JTextField. In the following program, I create a small GUI with a button and a textfield. When you press the button, it is supposed to set the text in the textfield to "Bob", but instead I get an error. The program works just fine up until it reaches the field.setText( "Bob" ) (I checked it by insertinga System.out.println( "Bob" ) ). It seems as though it cannot see the handle "field", but the handle should be in the scope of the method test(). Here is the the first part of the error trace:

JTextFieldTest.test(JTextFieldTest.java:51)
JTextFieldTest.access$000(JTextFieldTest.java:5)
JTextFieldTest$1.actionPerformed(JTextFieldTest.java:22)
...

After that it goes into javax.swing.* and java.awt.* errors.
Does anyone have any idea what the problem might be? Attached is a copy of my code.
Thanks for the help,
Don
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, your problem is really simple. YOu have a nullpointerexception. What is happening is this:
you have defined field 2 times. Once here:

And then again here:

So your test() method has access to the first, but not the second. And the first you never instantiated. So it is a nullPointerException.
Try declaring field only once, and it will work.
 
Don Gardner
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh! I didn't even realize I was doing that. No wonder field wasn't in scope! I guess I need to work on my proofreading skills.
Thanks for the help,
Don
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!