开发者

How to evaluate whether a string is null?

I have a string, which is read from a database. The string can be just "null". I need to decide whether it is null or not? Among the following ones, what is the appropriate way to do it?

String a = …;
If  (a == nu开发者_开发知识库ll)

If ( a.length == 0)

I also see something like

If  a.equals(“ “)

How does this work?


if (a == null)

In Java, == compares references, so you can check to see whether the reference a refers to null. a.length and a.equals would throw NullPointerExceptions if a equaled null, since you can't call methods on null.

When you compare Strings with equals, that:

is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

So even if we could call equals on null (which we can't, as mentioned before), it'd never return true anyway, as according to the documentation.

String.length would return zero only if a equaled "", an empty string. As according to the method:

Returns the length of this string. The length is equal to the number of Unicode code units in the string.

When in doubt, just test it out! :-)


This is how you check for a null String value in java:

String a = ...;
if (a == null) {
    // ...
}

The following throws a NullPointerException because a is null:

String a = null;
if (a.length() == 0) {
    // ...
}

The following checks for an empty string, which is different than null! (And if a is null, it will also throw a NullPointerException)

String a = ...;
if (a.equals("")) {
    // ...
}

The following (similar to above), checks for a one character string containing a single space character. (And if a is null, it will also throw a NullPointerException)

String a = ...;
if (a.equals(" ")) {
    // ...
}


This seems like something you could just easily test, but in Java you use

if( a == null ) ...

to test if a String is null.

Your second example should be

if( a.length() == 0)

which just tests to see if the String is empty, not null. The value "" would have a length of 0. That example would throw an exception if a really is null, because you can't call a method on a null reference in Java.

Your last example is testing to see if the String is a space character.


This depends entirely on how your database library interprets NULL values in the database.

Some may pass them through as a null string, some may pass them through as the empty string, some may throw an exception - it all depends on how you have it configured and what software you are using.

Without knowing what DB accessor you are using it is impossible to answer this question for certain.


I use ...

if(string == null || string.trim.length() == 0) {
 // string is null or empty
}

That being said there is also the NULL value from the database and that you should check when going through the ResultSet. Such as ...

String nameVal = rs.getString ("name");
   if (rs.wasNull ())
       nameVal = "(no name available)";

SQL is tristate in the case of Nullable columns. A column can have a value, A column can be NULL or a column can contain an empty String. NULL has a definite meaning that you need to keep in mind when programming database code.


if( a != null && a.length == 0 ) // Then it's empty


I would use if (a == null) for semantic reasons. Right there you are being very clear you are checking for a null string.


If the string can be the actual value "null" then use

if(a.equals("null"))

If the object can be set to NULL then use

if(a == null)

The best thing is to combine them

if(a == null || a.equals("null"))


If you have :

String test = getIntent().getStringExtra("value");

You should :

if ( test.equalsIgnoreCase("null")){
      dosomething...
}else {
       dosomething... 
}


try{
    methodWhichUsesTheString(a);
}catch (Exception NullPointerException){
    //string a seems to be null
    a = "Re-assign some desired value that would make things work" ;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜