Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Location
    Tokyo, Japan
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java - JTextField not null (but there is nothing in it)

    This should be pretty straight forward.

    I have a some JTextFields that I want to verify the contents of. IF the field is left blank it will add a message to my error window ELSE it will preform another check.

    The OTHER check works fine, but it never seems to give me the proper error message if the field is blank, it immediatly goes to the ELSE statement.

    If I do a System.out.println(variableName);, the variable appears to be NULL.

    Here is a piece of my code:
    Code:
    //animation delay is the name of the textfield
            animationDelay = this.animationDelay.getText();
    
            /* check if animation delay was left blank */
            if (animationDelay == null ||
                    animationDelay == ""){
                errMsg.add("Please enter disk Animation Delay");
            } else {
                /* check if the input given is valid */
                try {
                    /* parse the animation delay entered into an integer */
                    int animationDelayInt = Integer.parseInt(animationDelay);
                    /* check range of animation delay */
                    if (animationDelayInt < MIN_ANIMATION_DELAY){
                        errMsg.add("Animation Delay must be greater than 0");
                    } else if (animationDelayInt > MAX_ANIMATION_DELAY){
                        errMsg.add("Animation Delay must be less than " + MAX_ANIMATION_DELAY);
                    }
                } catch (NumberFormatException ex) {
                    errMsg.add("Animation Delay must be a Number");
                }
    if I manually set animationDelay = null; the proper error message is displayed.

    why is the blank field not being detected as null?
    Is there an invisible character that blank JTextFields insert in order to ruin my life?

    Thanks

    Yak

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Null is not the same as empty-- null means undefined, empty means defined and, well, empty

    So try if (animationDelay == "") instead, see if that works for you.

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    Tokyo, Japan
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Fumigator,

    Thanks for the suggestion, however if you look at the code I provided, I have already included that line.

    Code:
            /* check if animation delay was left blank */
            if (animationDelay == null ||
                    animationDelay == "")
    any other suggestions?

    Thanks,

    Yak

  • #4
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That isn't going to work at all since you are using a string class. When you use the == comparator on a class it is going to compare where in the java virtual memory thingy the two things are and since they are both not in the same spot it is going to return false. instead you should be using the equals method in the string class.

    if(animationDelay.equals("")) would work and you wouldn't even have to check for null since by default the JTextField is going to return a "" string.

  • #5
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    You also could compare the text that is inside the JTextField:

    example: animationDelay.getText() == ""

  • #6
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Quote Originally Posted by brad211987 View Post
    You also could compare the text that is inside the JTextField:

    example: animationDelay.getText() == ""
    That is actually a very dangerous idea. It would be better to use the .equals method through the string class as that is sure to work. With your way, it may not.

    with == you're comparing literals, address' even. But unless Java decides that getText() and "" belong in the same reference, then you're screwed. The only reason that this might work is because it's done at the same time, so java may see it and place it in the same reference in optimization.

    But there's no promise on that. Kura_kai was right. :P
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #7
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Inorder to use the == with a string you have to do alot of setup mumbo jumbo stuff which you get like aboue another 5-10 lines of code but it would just be easyer to use the .equals and stuff.

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Location
    Tokyo, Japan
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Excellent.

    Exactly the anwer I was looking for.

    Issue resolved by using Kura_kai's suggestion:
    Code:
    if(animationDelay.equals(""))
    Thank you Kura_kai, and everyone else who provided thier input.

    Yak


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •