-3

I am .net programmer and completely new in java. I am facing problem in handling null string in java. I am assigning value from string array to string variable completeddate. I tried all this but that didn't work.

String COMPLETEDATE;
COMPLETEDATE = country[23];

if(country[23] == null && country[23].length() == 0) 
{
    // ...
}

if (COMPLETEDATE.equals("null")) 
{
    // ...
}

if(COMPLETEDATE== null)
{
    // ...
}

if(COMPLETEDATE == null || COMPLETEDATE.equals("null"))
{
    // ...
}
Flexo
  • 87,323
  • 22
  • 191
  • 272
seema
  • 13
  • 1
  • 1
  • 5
  • 4
    `country[23] == null && country[23].length() == 0` - that's a sure-fire way to get a `NullPointerException` if I've ever seen one. – Makoto Apr 07 '14 at 02:37
  • [How to compare strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java?rq=1) –  Apr 07 '14 at 02:38
  • 1
    @JarrodRoberson: It doesn't feel like this is a string comparison issue; this is an "object vs null" comparison issue. – Makoto Apr 07 '14 at 02:44
  • 1
    In Java you are not allowed to call methods on a `null` object. So, if `obj==null` then `obj.anyMethod()` produces [`NPE`](http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html). – PM 77-1 Apr 07 '14 at 02:44
  • 2
    From the looks of your last edit, you completely and drastically changed the scope of the question. If you have another question to ask, create a new question to avoid potentially invalidating existing answers. – Makoto Apr 07 '14 at 03:46

4 Answers4

3

For starters...the safest way to compare a String against a potentially null value is to put the guaranteed not-null String first, and call .equals on that:

if("constantString".equals(COMPLETEDDATE)) {
    // logic
}

But in general, your approach isn't correct.

The first one, as I commented, will always generate a NullPointerException is it's evaluated past country[23] == null. If it's null, it doesn't have a .length property. You probably meant to call country[23] != null instead.

The second approach only compares it against the literal string "null", which may or may not be true given the scope of your program. Also, if COMPLETEDDATE itself is null, it will fail - in that case, you would rectify it as I described above.

Your third approach is correct in the sense that it's the only thing checking against null. Typically though, you would want to do some logic if the object you wanted wasn't null.

Your fourth approach is correct by accident; if COMPLETEDDATE is actually null, the OR will short-circuit. It could also be true if COMPLETEDDATE was equal to the literal "null".

Makoto
  • 104,088
  • 27
  • 192
  • 230
1

To check null string you can use Optional in Java 8 as below: import Optional

import java.util.Optional;

import it as above

String str= null;
Optional<String> str2 = Optional.ofNullable(str);

then use isPresent() , it will return false if str2 contains NULL otherwise true

if(str2.isPresent())
{
//If No NULL 
}
else
{
//If NULL
}

reference: https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html

nomadSK25
  • 2,350
  • 3
  • 25
  • 36
0

It is not entirely clear what you are asking, but to check if a String variable is null, use the following statement.

if(myString==null)

This checks whether the object reference is null.

The following statement, which you have written is incorrect for two reasons.

if (COMPLETEDATE.equals("null")) 
                    {
                        // ...

                    }

1. null is a keyword in Java, "null" is just a string of text.

2. .equals() checks to see if two objects are equal according to the given method's definition of equality. Null checks should always be made using the == comparison operator, as it checks reference equality.

0

If a variable is null, you cannot dereference it.

That means you can not invoke methods on it.

So... The following if statement will throw a NullPointerException every time the first clause is true:

if (a == null && a.length() == 0)

In other words: if a is null, you CANNOT invoke the length method on a.

jahroy
  • 22,322
  • 9
  • 59
  • 108