Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error Message

 
Candy Bortniker
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • 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.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • 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
 
Gary McGath
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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();
}
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic