• 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

Error Message

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to put an error message out to a panel but my statement isn't working and it is just like my example. Do you have any suggestions?
if (Shares.getText().equals(null))
{
System.out.println("I get here.");
ErrorMsg.setText("Shares is missing.");
System.out.println(ErrorMsg.getText());
}
The first line prints that message but I don't get anything on the panel or last line. ErrorMsg is a label.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi candy,
we have to get the text value from the textfield and compare it like,
textfield.getText().equals("")
not with null as you are doing...
try this and see if it works..
regards
maulin
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand you correctly, your log or console is printing out "I get here.", but nothing further after that. This suggests that the comparison to null is working (though using an == comparison would be a tiny bit more efficient), but the setText call is throwing an exception. I'd put a try{} around the whole thing, followed by
catch (Exception e) {
e.printStackTrace();
}
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you are getting an exception on ErrorMsg.setText, it is most likely a NullPointerException. Is ErrorMsg already initialized and added to the GUI?
 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not getting an exception. I just not getting anything.
 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, changing the if statement to
textfield.getText().equals("")
fixed the problem. I didn't think it would because the statement seemed to be working as it was but now it works as it should. Thank you so much.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So when you said "The first line prints that message", what were you talking about? Is it possible that there's some other part of the program that also prints "I get here" and you were confused by this?
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic