73

I am passing an accountid as input from an XML file as shown, which will be parsed later and will be used in our code:

<accountid>123456</accountid>
<user>pavan</user>

The issue is that if nothing is passed (null value in accoutnid) is passed as accountid, I could not able to handle that situation in Java code. I tried this but I was not successful:

if (acct != null||acct==""||acct.equals("")) 
{
    // the above is not working 
}

I was able to handle this successfully using the following approach:

if(!acct.isEmpty())
{
   // thisis working 
}

Can we rely on the String.isEmpty() method for checking the null condition of a String? Is this valid?

0

7 Answers 7

172

No, absolutely not - because if acct is null, it won't even get to isEmpty... it will immediately throw a NullPointerException.

Your test should be:

if (acct != null && !acct.isEmpty())

Note the use of && here, rather than your || in the previous code; also note how in your previous code, your conditions were wrong anyway - even with && you would only have entered the if body if acct was an empty string.

Alternatively, using Guava:

if (!Strings.isNullOrEmpty(acct))
Sign up to request clarification or add additional context in comments.

7 Comments

Some also like this version of the same check: "".equals(acct)
Hi Jon , I tried with a sample Program , String str = ""; System.out.println(str.isEmpty()); , here it returned true , it didn't throw NullPointerException . so why cant we use isEmpty for Checking Null values ?? Could you please clarify this ??
@Kiran: Because "" isn't the same as a null reference. Try it with String str = null; and then you'll get a NullPointerException. It's very important to understand the difference between a string reference which is null and a string reference which refers to an empty string.
@AlexZam: You'd still want to also check whether it's null. The point is to check whether the string has any content.
Thank you Jon , i tried with this code you told , if (acct != null && !acct.isEmpty()) , but still it is not able to handle when no accountid is passed .
|
37

Use StringUtils.isEmpty instead, it will also check for null.

Examples are:

 StringUtils.isEmpty(null)      = true
 StringUtils.isEmpty("")        = true
 StringUtils.isEmpty(" ")       = false
 StringUtils.isEmpty("bob")     = false
 StringUtils.isEmpty("  bob  ") = false

See more on official Documentation on String Utils.

3 Comments

Link only answers should be posted as comments instead
but therefore i need more reputation
StringUtils.isEmpty() is deprecated now!!! ;(
9

You can't use String.isEmpty() if it is null. Best is to have your own method to check null or empty.

public static boolean isBlankOrNull(String str) {
    return (str == null || "".equals(str.trim()));
}

4 Comments

I would recommend using StringUtils from commons-lang instead of writing your own.
+1 I would not recommend adding commons-lang to your project (unless you are already including it) only for such a simple method.
Reading the above answers, how about str == null || str.trim().isEmpty()?
@ardila which is exactly what this answer is checking
3

No, the String.isEmpty() method looks as following:

public boolean isEmpty() {
    return this.value.length == 0;
}

as you can see it checks the length of the string so you definitely have to check if the string is null before.

Comments

2

I think the even shorter answer that you'll like is: StringUtils.isBlank(acct);

From the documentation: http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#isBlank%28java.lang.String%29

isBlank
public static boolean isBlank(String str)
Checks if a String is whitespace, empty ("") or null.

 StringUtils.isBlank(null)      = true
 StringUtils.isBlank("")        = true
 StringUtils.isBlank(" ")       = true
 StringUtils.isBlank("bob")     = false
 StringUtils.isBlank("  bob  ") = false
 
Parameters:
str - the String to check, may be null
Returns:
true if the String is null, empty or whitespace

Comments

1
String s1=""; // empty string assigned to s1 , s1 has length 0, it holds a value of no length string

String s2=null; // absolutely nothing, it holds no value, you are not assigning any value to s2

so null is not the same as empty.

hope that helps!!!

1 Comment

The Question is not about whether Null and empty are same. The OP is instead asking if he can rely on String isEmpty method to check for null.
0

You could use TextUtils.isEmpty to check both nullable and empty string.

TextUtils.isEmpty(acct)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.