I have wriiten a method like this
public ArrayList<T> GetDoctorDetail(String name)
{
if (name!=null || !name.isEmpty() || name!="")
{
//Statements
}
}
but in eclipse !name by underline with a yellow line show
Null pointer access: The variable name can only be null at this location.
why? and what is the solution.
If
nameis non-null, the conditional||operator won’t evaluate the second operand at all. So the only case in which the second operand can be evaluated is whennameis null, in which case it will throw.I suspect you want
Or possibly:
Note that comparing strings with
==and!=is also almost always the wrong thing to do, as it compares references. You would normally useequalsinstead. Not only that, but it would be useless anyway here – it could only be equal to “” if it’s empty, so it’s the exact same condition as the second operand.