Setting JTextFields

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 ]

Welcome to JavaRanch, Chris! It must be that you haven't properly initialized the TextField identifier(s). You'd require a statement not unlikeTextField 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.

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?)