Problem with if condition when test null

Hi guy,

I have a very nice problem with the if condition:

I write this statement

......

Strig location;

......

if ((location == "") || (location == "builder") || (location == "MF - CF - TT - 00")) {....

It's obviuse!

I believed that if the location variable was null the statement gone inside, but .... sigh isn't so.

I see that if a condition of null (location == "") is tested together an OR operator and other conditions it's not working.

I try with .equals and null instead "" also.

While, if i test first all the conditions without null condition and then test null condition after with another if statement..... magic! it work!

if ((location == "builder") || (location == "MF - CF - TT - 00")) {....

......

}

elseif ((location == ""){....

It's strange?No?

Thanks for help...

Bye.

[928 byte] By [mbonelli] at [2007-9-27 2:19:58]
# 1
did u try if(location == null) {?
newgrad at 2007-7-4 21:38:36 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 2
yes!!
mbonelli at 2007-7-4 21:38:36 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 3

If you want to test if a string is null or blank (null is not the same as "") before you test if your string equals "builder" or "MF - CF - TT - 00" you could do something like this :

if (location == null || (location.trim()).length() == 0) {

// this string is blank or null

} else if (location.equals("builder")) {

// this string equals "builder"

} else if (location.equals("MF - CF - TT - 00")) {

// this string equals "MF - CF - TT - 00"

}

luke_hillary at 2007-7-4 21:38:36 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...
# 4
Also, the earlier posts should have mentioned this, don't use == to compare the contents of two Strings. It doesn't work. Uselocation.equals("builder")instead.
DrClap at 2007-7-4 21:38:36 > top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...