Java: How to check if an object is null?

I am creating an application that retrieves images from the Internet. If it is impossible to obtain an image, you must use another local image.

When trying to execute the following lines:

Drawable drawable = Common.getDrawableFromUrl(this, product.getMapPath()); if (drawable.equals(null)) { drawable = getRandomDrawable(); } 

The string if (drawable.equals (null)) throws an exception if drawable is null.

Does anyone know how to check the drawable value so as not to throw an exception if it is null and get a local image (do drawable = getRandomDrawable ())?

+57
java android
Jan 09 '10 at
source share
8 answers

Edited solution for Java 8:

 final Drawable drawable = Optional.ofNullable(Common.getDrawableFromUrl(this, product.getMapPath())) .orElseGet(() -> getRandomDrawable()); 

In this case, you can declare drawable final .

As Chasmo noted, Android does not support Java 8 at the moment. Therefore, this solution is possible only in other contexts.

+28
Jan 09 '10 at 11:10
source share
 Drawable drawable = Common.getDrawableFromUrl(this, product.getMapPath()); if (drawable == null) { drawable = getRandomDrawable(); } 

The equals() method checks for equality of values, which means that it compares the contents of two objects. Since null not an object, it crashes when you try to compare the contents of your object with the contents of null .

The == operator checks for equality of links, which means that it checks to see if two objects are actually the same object. This does not require objects to really exist; two non-existent objects ( null references) are also equal.

+144
Jan 09 '10 at
source share

I use this approach:

 if (null == drawable) { //do stuff } else { //other things } 

This method, which I find, improves the readability of the line - since I quickly read the source file, I see that this is a zero check.

Regarding why you cannot call .equals() on an object that may be null ; if you have a reference to an object (namely "drawable") null , it does not point to an object on the heap. This means that there is no object on the heap on which the equals() call can end.

Good luck

+14
Jan 09
source share

DIY

 private boolean isNull(Object obj) { return obj == null; } 



 Drawable drawable = Common.getDrawableFromUrl(this, product.getMapPath()); if (isNull(drawable)) { drawable = getRandomDrawable(); } 
+7
Feb 17 '14 at 1:51
source share

if (yourObject instanceof yourClassName) will evaluate to false if yourObject is null .

+5
Sep 27
source share
 drawable.equals(null) 

The above line calls the "equals (...)" method on the object to be drawn.

So, when drawable is not null and is a real object, then everything goes well, since calling the method "equals (null)" returns "false"

But when "drawable" is NULL, it means calling the method "equals (...)" for a null object, means calling a method for an object that does not exist, therefore it throws a "NullPointerException"

To check if an object exists and is not null, use the following

 if(drawable == null) { ... ... } 

In the above state, we check that the reference variable "drawable" is null or contains some value (a reference to its object), so it will not throw an exception if drawable is null, checking

 null == null 

.

+3
Jan 09
source share

It is probably a bit more efficient to catch a NullPointerException. The above methods mean that the runtime double-checks null pointers.

0
Jan 09 '10 at 15:13
source share

Use google guava libs to handle is-null-check (deamon update)

 Drawable drawable = Optional.of(Common.getDrawableFromUrl(this, product.getMapPath())).or(getRandomDrawable()); 
0
May 24 '13 at 12:50
source share



All Articles