I have an if statement which is returning a NullPointerException.
Cause: it is comparing two objects, one of them is null:
Object obj;
if (obj.toString.equals("string"))
{ //do something }
else
{ //do another something }
How I know obj is null?
If I try to print that object (System.out.println(obj.toString()), logically I get a NullPointer as well. When I click the error, instead of selecting the corresponding line to the if statement, it selects my System.out statement, indicating that the error source is my object (also, I'm veryfing whether the object returns as null from my database, which in my case, a "null" keyword takes place where the "string" is in my statement).
Taking in consideration that I do know my obj is null (even by checking that it is null in my database), it is not possible to compare it with another object, since apparently if statements are unable to compare objects when one of them is null.
I know that by checking the following code (as described above):
Object obj;
if (obj.toString.equals(null))
{ //do something }
Shouldn't that statement return true? Cause it is not doing what is inside that if block.
Mainly, when the compiler reads if (obj.toString.equals("string")), since it is false (cause obj doesn't equal to "string") that should fall into my else block but that's not happening.
Any thoughts?
for instance, when I say that I click the error and it leds me to the if statement line (or my System.out line, for object verification), that's what I mean: