View Full Version : Java - JTextField not null (but there is nothing in it)

09-14-2006, 04:32 AM
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:

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



09-14-2006, 07:44 AM
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.

09-19-2006, 07:04 AM

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

/* check if animation delay was left blank */
if (animationDelay == null ||
animationDelay == "")

any other suggestions?



09-19-2006, 02:16 PM
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.

09-19-2006, 10:18 PM
You also could compare the text that is inside the JTextField:

example: animationDelay.getText() == ""

09-19-2006, 11:05 PM
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

09-19-2006, 11:59 PM
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.

09-20-2006, 06:41 AM

Exactly the anwer I was looking for.

Issue resolved by using Kura_kai's suggestion:


Thank you Kura_kai, and everyone else who provided thier input.