Reputation: 5633
String says its not null but then later throw NullPointerException
I had this problem(see the link), where I was sure that a String is null, but in fact the String was "null"
jcasso told me:
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.
When this situations appear?
Upvotes: 0
Views: 254
Reputation: 1502006
Mostly when you use string concatenation or String.valueOf()
:
String x = null;
String y = x + ""; // y = "null"
String z = String.valueOf(x); // z = "null"
(There are similar variants, such as using StringBuilder.append((String) null)
.)
Upvotes: 1