• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to set text in a text field..

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem SOLVED




 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I just had the craziest dream. This tiny ad was in it.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic