This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Setting JTextFields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Setting JTextFields" Watch "Setting JTextFields" New topic
Author

Setting JTextFields

Chris Sanker
Greenhorn

Joined: Jan 27, 2003
Posts: 5
When I use a FieldName.setText("" + variable), I get a NullPointerException. I have tried declaring the fields as JTextFields and TextFields but neither work. What can I do to eliminate this problem?
[ January 27, 2003: Message edited by: Chris Sanker ]
Chris Sanker
Greenhorn

Joined: Jan 27, 2003
Posts: 5
Here is the portion of the code where the error occurs.

[ January 27, 2003: Message edited by: Chris Sanker ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch, Chris!
It must be that you haven't properly initialized the TextField identifier(s). You'd require a statement not unlike
TextField myTextField = new TextField("");
If the variable myTextField were simply declared but never initialized (with an assignment statement using an equals sign), then using it to invoke a method such as setText would likely result in a NullPointerException. Making any sense?
Perhaps you could post a complete (and simple) example of some code that you're having a problem with.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Chris Sanker
Greenhorn

Joined: Jan 27, 2003
Posts: 5
Here is a sample code that doesn't work:
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
You've declared an instance TextField variable named textField and you've declared a different local TextField variable in the addWidgets method with the same name(textField). These variables have the same name, but they are different variables. The one declared in the method is properly declared and initialized, but it is only accessible in the method. The instance variable was never explicitly initialized, so the compiler steps in and makes it a null reference. When you try to use it, you get yer NullPointerException.
So, you've discovered that it's possible to declare a variable in a method with the same name as a variable declared outside of any method, and you've discovered how this might cause some confusion. Have you understood the situation yet and know how to fix it, or are you ready for a little bit more of a push in the right direction? (Hint: What would it mean to remove the declaration of the TextField variable in the addWidgets method?)
Chris Sanker
Greenhorn

Joined: Jan 27, 2003
Posts: 5
Dude, you rock.
Thanks a lot!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting JTextFields
 
Similar Threads
multidimensional arraylist of JTextFields
GUI Pieces and Parts Not Sticking! Help!
need help in searchin
setting taborder
Reset All Fields in Add User Screen