I am checking to see if an object is null before using it like this
if (resturant.getLat() != null)
//do stuff
But it is throwing a java.lang.NullPointerException
how can I check that restaurant.getLat() is not null?
Thanks
I am checking to see if an object is null before using it like this
if (resturant.getLat() != null)
//do stuff
But it is throwing a java.lang.NullPointerException
how can I check that restaurant.getLat() is not null?
Thanks
You need to write if (restaurant != null && restaurant.getLat() != null)
This is because restaurant
itself might be null
.
If
statements in Java are evaluated from left to right &&
is short-circutted; meaning that once the value of the expression is known, evaluation stops. So the way I've written it is idiomatic.