By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,651 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,651 IT Pros & Developers. It's quick & easy.

How can I convert a JTextField that's returning null to a different string value?

P: 9
I have a JTextField that I'm not always passing a value to, but it causes an exception to be thrown when the program runs if the person hasn't entered anything into the JTextField. I have some code below that I'm trying to convert a null response into a different String value. Why isn't it assigning stringInput as a String of "0"?

Expand|Select|Wrap|Line Numbers
  1.             String stringInput = " ";
  2.             stringInput = dinnerTextField.getText();
  3.             if (stringInput == null)
  4.                stringInput = "0";
Mar 5 '12 #1
Share this Question
Share on Google+
1 Reply

P: 13,264
Maybe stringInput is the empty string "" rather than null.
To handle that you could use

Expand|Select|Wrap|Line Numbers
  1. if(stringInput == null || stringInput.trim().equals("")) {
Mar 5 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.