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.
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
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(); }
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?
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.
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?
"I'm not back." - Bill Harding, Twister
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