Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to set text in a text field..  RSS feed

 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,




i am trying to set the value of text field of above class (DemoSw.java) from other class which i have written below class (PopupListener.java)






PROBLEM :: When my program executes the setTimeField() Method in CLASS :: DemoSw which is at LINE 0 & which i invoked from LINE 2 the CLASS :: PopupListener ()
*** its giving me NULLPOINTER EXCEPTION AT THE LINE 1 ******



don't know why
[ May 16, 2008: Message edited by: Rahul Shilpakar ]
 
Jai Muthunalla
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul, are you sure that the String parameter - timeToSet is not NULL? I would suggest you check that first
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem SOLVED




 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes that was not a null value.

the problem was i have no initialization in DemoSw method.
So when ever create new object of the DemoSw it was taking new context to set the value of the Text Field which has not yet intialized and was only declared. It will initialize only when the startMethod is excuted. and the same method was not executing when i try to set the text in that Text field.
 
Campbell Ritchie
Sheriff
Posts: 53750
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through the fora and search for NullPointerException (NPE); everybody gets them, everybody gets annoyed about them, and everybody needs to know how to handle them. The hard part is finding out why they happen.

You have some very unconventional code in your class; you are instantiating a field (which should, by the way, be declared "private") in a method rather than in a constructor. Please instantiate your field in the constructor and call it by saying new DemoSw();.

You have got a strange method in your mouseListener class. A MouseAdapter doesn't have an actionPerformed method, so that is an inappropriate class to extend. Go through these fora and you will find discussions about how to use ActionListeners, eg here.
If you have a MenuItem, it will support ActionListeners, so you don't need a new class.

And put in a line just before the line where the NPE occurs like this:
System.out.printf("timeToSet == null: %b. jTimefld == null %b%n", timeToSet == null, jTimefld == null);
If you get "false" for both, then the problem lies elsewhere.

Please use ctrl-C ctrl-V to quote code; it is much easier to handle when we know we have the exact code which is causing problems: PostRealCode.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jai Muthunalla:
Rahul, are you sure that the String parameter - timeToSet is not NULL? I would suggest you check that first

It wouldn't matter. JTextField and most other text components can handle having null set as the text. It will clear the text, and do the same as setting an empty string as the text.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!