The line says it's not null and then throws a NullPointerException
OMG. I have a little project and the Strings are killing me!
Now I have a string that is null
(takes a value from a call getParameter()
from a servlet).
The problem is that I am trying to check if it is null, and even if it is null, the program tells me that it is not null
, but later in the program, when I use the variable, I get an exception stating that it is a variable 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 am I doing wrong?
Only the fourth condition works! How about the rest (except for the second, because I set this condition, because I was in despair)
I taught ID == null
or ID.equals(null)
will be fine, but no.
Edit:
The problem is that I am getting the id value from the form (form 1 is normal). But in this case I am using form 2 which has no ID inputs, so the ID should be null
, not"null"
source to share
Since you are getting the string from the servlet, I can tell that this is ok.
Java converts empty string to string "null" under some conditions.
Obviously the string you are retrieving is not a null value, but it is a 4 char "null" string
Why don't you try debugging? Or just see what this returns:
System.out.println("Length of ID: " + ID.Length);
Edit: if no exception is specified here it means the string is not null, and also outputting "Length ID: 4" means the string is indeed ID = "null"
EDIT2: Okay some guys don't understand what's going on here and they say how can an empty string be "null" in some Java condition? They find it ridiculus. I prefer them to try this in java:
String abc = null;
String xyz = "hello";
System.out.println(xyz + abc);
The output will be "hellonull" Nothing else ...
We also have a servlet. There is zero information. The servlet sends null data as "null", what should it do? Empty line? Come on !!! "
source to share
ID.equals("null")
Clearly ID
contains a four letter string "null"
. So it is not null
(meaning for "nothing").
For more information on the constant, null
see the Java glossary . Basically a variable has a value null
if it does not refer to any object. A string "null"
is an object, namely an instance of the String class , in which case the variable ID
refers to that object.
(Note that, by convention, Java variables start with a lowercase letter, and abbreviations like ID are written in all lowercase, so write ID
instead ID
.)
source to share
Here are the four tests you tried. The first and fourth are the only ones you will need.
-
ID == null
: is this field 'ID' null? -
ID == "null"
: ref for field 'ID' the same as the newly allocatedString
'null'? This usually returns false. -
ID.equals(null)
: this should always return false - conceptually it would be true, you should throwNullPointerException
. -
ID.equals("null")
: is theString
'ID' value the same as theString
"null" value ?
source to share
If the result was actually zero, then
ID == null
would be sufficient, but as mentioned the ID string value is obviously "null" and not a null object.
You should be using .equals when comparing strings, not using == This blog explains more about it: http://blog.enrii.com/2006/03/15/java-string-equality-common-mistake/
So "null" is not zero, in Java.
source to share
If the value comes from a servlet, the container will most likely convert an empty form field to an empty string. You must check against null and blank ("").
if (value==null || value.equals(""))
Alternatively, you can use the String method isEmpty()
:
if (value==null || value.isEmpty())
source to share
If the value of your variable id is the string literal "null", I would assume that there is an error in your code when you retrieve it using the getParameter () method. According to the docs, the getParameter () method should return null (null reference) if there is no value for the specified name. This indicates that somewhere you are performing an operation that converts the result to a string literal, possibly a concatenation to an empty string (i.e. ID + "";)
source to share