views:

255

answers:

9

I want to perform some action ONLY IF my string has a meaningful value. So, I tried this.

if (!myString.equals("")) {
doSomething
}

and this

if (!myString.equals(null)) {
doSomething
}

and this

if ( (!myString.equals("")) && (!myString.equals(null))) {
doSomething
}

and this

if ( (!myString.equals("")) && (myString!=null)) {
doSomething
}

and this

if ( myString.length()>0) {
doSomething
}

And in all cases my program doSomething in spite on the fact that my string IS EMPTY. It equals to null. So, what is wrong with that?

ADDED:

I found the reason of the problem. The variable was declared as a string and, as a consequence, null assigned to this variable was transformed to "null"! So, if (!myString.equals("null")) works.

+7  A: 
if (myString != null && !myString.isEmpty()) {
  // doSomething
}

As further comment, you should be aware of this term in the equals contract:

From Object.equals(Object):

For any non-null reference value x, x.equals(null) should return false.

The way to compare with null is to use x == null and x != null.

Moreover, x.field and x.method() throws NullPointerException if x == null.

polygenelubricants
You should do length check as opposed to equals for efficiency purposes. Check this link http://hanuska.blogspot.com/2006/08/empty-string.html
CoolBeans
I would say you should do `s.isEmpty()` instead of `s.length() == 0` for readability. Any difference in performance is negligible. I do agree that `s.equals("")` is terrible.
polygenelubricants
I agree on the readability. It depends whether we are using Java 5 vs Java 6 since isEmpty() is only in Java 6.
CoolBeans
@CoolBeans: Good comment! I hadn't realized that! I'm a bit late to the Java party =)
polygenelubricants
A: 

You misspelled EQUALS in all those examples.

Catdirt
Should be a comment.
polygenelubricants
You can't comment with rep under 100. =/
Catdirt
Ah, sorry. I'd love to help but this shouldn't get upvotes on principle.
polygenelubricants
A: 

You need to check that the myString object is null

if (myString != null) {
doSomething
}
WhirlWind
A: 

Try,

myString!=null && myString.length()>0
Bragboy
+1  A: 

If myString is null, then calling myString.equals(null) or myString.equals("") will fail with a NullPointerException. You cannot call any instance methods on a null variable.

Check for null first like this:

if (myString != null && !myString.equals("")) {
    //do something
}

This makes use of short-circuit evaluation to not attempt the .equals if myString fails the null check.

Michael Myers
+1 for short-circuit; it's essential for the expression to work. However, you really should use `.isEmpty()` instead of `.equals("")`.
polygenelubricants
Okay I meant to leave my comment on this post. lol. -> " You should do length check as opposed to equals for efficiency purposes. Check this link hanuska.blogspot.com/2006/08/empty-string.html "
CoolBeans
@CoolBeans: Sure, and with Java 6 you could even do `myString.isEmpty()`. But at this level, readability trumps optimization, and people might be more used to reading `myString.equals("")`. (Or possibly not.)
Michael Myers
+1  A: 

If your string is null, calls like this should throw a NullReferenceException:

myString.equals(null)

But anyway, I think a method like this is what you want:

public static class StringUtils
{
    public static bool isNullOrEmpty(String myString)
    {
         return myString == null || "".equals(myString);
    }
}

Then in your code, you can do things like this:

if (!StringUtils.isNullOrEmpty(myString))
{
    doSomething();
}
Andy White
A: 

This should work:

if (myString != null && !myString.equals(""))
    doSomething
}

If not, then myString likely has a value that you are not expecting. Try printing it out like this:

System.out.println("+" + myString + "+");

Using the '+' symbols to surround the string will show you if there is extra whitespace in there that you're not accounting for.

elduff
A: 
 if (myString != null && myString.length() > 0) {

        // your magic here

 }

Incidently, if you are doing much string manipulation, there's a great Spring class with all sorts of useful methods:

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/util/StringUtils.html

Richard
A: 

I'd do something like this:

( myString != null && myString.length() > 0 )
    ? doSomething() : System.out.println("Non valid String");
  • Testing for null checks whether myString contains an instance of String.
  • length() returns the length and is equivalent to equals("").
  • Checking if myString is null first will avoid a NullPointerException.
James P.