views:

446

answers:

7

OMG. I have a little project to do and the Strings are killing me!

Now, I have a String which is null (is taken the value from invoking getParameter() from a servlet).

The problem is that, I'm trying to see if it's null, and, even if it's null, in the program is telling me that is not null, but later in program, when I'm using the variable, I receive a exception saying the variable is null.

    System.out.println("In  " + ID); // in console: In null
    if ((ID == null) || (ID == "null") || ID.equals(null) || **ID.equals("null")**)
    {

       // after I put the 4th condition, the if is working right (WHAT IS THE PROBLEM?)            
        System.out.println("==null");
        this.ID = "";
    }
    else
    {
        System.out.println("!=null");
        this.ID = ID;
    }
    System.out.println("After " + ID);

What I'm doing wrong?

Only the forth condition is working! What about the rest(except second one, because that condition i put it because I was desperate)

I taught (ID == null) or ID.equals(null) will be ok, but noh.

Later edit: The problem is that, I'm getting the value of the ID from a form(form 1 let's say- usually). But in this case, I'm using form 2 which doesn't have any ID inputs, so ID must be null and not "null"

+2  A: 

Looks like it is return the String "null" and not a Null Object.

Gandalf
You mean a null *reference*, which is a value which doesn't refer to *any* object.
Jon Skeet
The problem that, I'm getting the value of the **ID** from a form(**form 1** let's say).But in this case, I'm using **form 2** which doesn't have any ID inputs.
cc
@Skeet of course, I can see how that could be confusing...
Gandalf
+9  A: 
ID.equals("null")

Clearly, ID contains the four-letter string "null". So it's not null (the value for "nothing").

See the Java glossary for more on the null constant. Basically a variable has the value null if it does not reference any object. The string "null" is an object however, namely an instance of the class String, and in this case the variable ID references this object.

(Note that by convention Java variables start with a lower case letter, and acronyms like ID are written completely lower case, so write id instead of ID.)

Stephan202
A: 

If the result was actually a null then

ID == null

would suffice, but as is mentioned the string value of ID is obviously "null" rather than the null object.

You should use .equals when comparing strings rather than using == This blog explains more about his: http://blog.enrii.com/2006/03/15/java-string-equality-common-mistake/

So, "null" does not equal null, in Java.

James Black
+6  A: 

Here are the four tests you've tried. The first and the fourth are the only ones that you should need.

  • ID == null : is the field 'ID' null?
  • ID == "null": is the ref for the field 'ID' the same as the newly allocated String "null"? This should generally return false.
  • ID.equals(null): this should always return false - conceptually were this ever true you should throw a NullPointerException.
  • ID.equals("null"): is the value of the String 'ID' the same as the value of the String "null"?
akf
+2  A: 

Since you get the string from a servlet i can say that this is normal.

Java converts a null string to a "null" string on some conditions.

Obviously the string you retrieve is not a null value, but it is a 4 char string "null"

Why don't you try debugging? Or just see what does this return:

System.out.println("Length of ID:  " + ID.Length);

Edit: If you don't get exception here, this means that the string is not null and also output "Length of ID: 4" will mean that the string is really ID = "null"

EDIT2: Alright it seems that some guys do not understand what is going on here and they say how can a null string be "null" in some conditions in Java? They find it riddiculus. I prefer them to try this on java:

String abc = null;
String xyz = "hello"; 
System.out.println(xyz + abc);

The output will be "hellonull" Nothing else...

Also here we have a servlet. There is a null data. Servlet sends the null data as "null" what should it do? An empty string? Come on!!! "

JCasso
Yes, you're rite, the id variable contains "null". P.S. 1. Thanx to everyone for telling not to use "==" and that variable contains "null".
cc
A: 

If the value is coming from a Servlet then the container is most likely converting an empty form field to a blank string. You should do a check against null and blank ("").

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

Alternatively you can use String's isEmpty() method:

if (value==null || value.isEmpty())
Steve Kuo
A: 

If the value of your variable ID is the string literal "null", then I would guess that there is a bug earlier in the code when you retrieve it using the getParameter() method. According to the docs, the getParameter() method is supposed to return null (the null reference) if there is no value for the specified name. This indicates that somewhere you are doing an operation that converts the result to the string literal, perhaps concatinating with the empty string (i.e. ID + "" ; )

John Haager