Java: How to check if object is null?

前端 未结 9 1318
甜味超标
甜味超标 2021-01-30 08:11

I am creating an application which retrieves images from the web. In case the image cannot be retrieved another local image should be used.

While trying to execute the f

9条回答
  •  情话喂你
    2021-01-30 08:33

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

    The equals() method checks for value equality, which means that it compares the contents of two objects. Since null is not an object, this crashes when trying to compare the contents of your object to the contents of null.

    The == operator checks for reference equality, which means that it looks whether the two objects are actually the very same object. This does not require the objects to actually exist; two nonexistent objects (null references) are also equal.

提交回复
热议问题