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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jerick
    New Member
    • Feb 2012
    • 9

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

    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"?

    Code:
                String stringInput = " ";
                stringInput = dinnerTextField.getText();
                if (stringInput == null)
                   stringInput = "0";
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Maybe stringInput is the empty string "" rather than null.
    To handle that you could use

    Code:
    if(stringInput == null || stringInput.trim().equals("")) {

    Comment

    Working...