0

How to check null String

First thing to remember is that the == operator compares two object references to see whether they refer to the same instance whereas equals() method compares the chars inside a String object.

Thus, str.equals(null) will always return false because there is no such object reference as null in reality.
So only str==null can help you.

However, a more strong way to check if a str object contains any char or not is

if str==null str.equals("")
{
...
}

Other way is just perform the operation on the str in a try-catch block catching the NullPointerException like
try {
str.substring(0, 2);
}catch(NullPointerException)
{
e.printStackTrace();
}

|

0 Comments

Copyright © 2009 So That I Can Remember All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.