Rifki Maulana
Rifki Maulana

Reputation: 153

How to handle NULL value in JSON Object using retrofit

I try to get a value from json using retrofit like this

holder.txtColor.setText(dataList.get(position).getProductColorMotor().getColorName());

But i got an error like this :

java.lang.String com.qiscus.qismo.chat.data.model.ordermodel.ProductColorResponse.getColorName()' on a null object reference

I assume it's because i got null from the JSON response :

...
      "product_color_motor": null
...

The response will sometimes return NULL.

But normally the response is look like this :

...
"product_color_motor": {
            "id": 315,
            "product_variant_id": 9,
            "md_model_code": "GW2",
            "main_dealer_type": "Beat Pop ESP CW",
            "color_id": 86,
            "color_code": "PM",
            "color_name": "Groovy Red White",
            "color_hex_1": "#FFFFFF",
            "color_hex_2": "#CC0000",
            "parent_color": "Groovy Red White,Groovy,Red,White"
        }
...

OrderListResponse.java

public class OrderListResponse {
@SerializedName("product_color_motor")
@Expose
private ProductColorResponse productColorMotor;

public ProductColorResponse getProductColorMotor() {
        return productColorMotor;
}

public void setProductColorMotor(ProductColorResponse productColorMotor) {
        this.productColorMotor = productColorMotor;
}

ProductColorResponse.java

public class ProductColorResponse {
    @SerializedName("product_variant_id")
    @Expose
    private Integer productVariantId;
    @SerializedName("color_id")
    @Expose
    private Integer colorId;
    @SerializedName("color_code")
    @Expose
    private String colorCode;
    @SerializedName("color_name")
    @Expose
    private String colorName;
    @SerializedName("color_hex_1")
    @Expose
    private String colorHex1;
    @SerializedName("color_hex_2")
    @Expose
    private String colorHex2;
    @SerializedName("parent_color")
    @Expose
    private String parentColor;

    public Integer getProductVariantId() {
        return productVariantId;
    }

    public void setProductVariantId(Integer productVariantId) {
        this.productVariantId = productVariantId;
    }

    public Integer getColorId() {
        return colorId;
    }

    public void setColorId(Integer colorId) {
        this.colorId = colorId;
    }

    public String getColorCode() {
        return colorCode;
    }

    public void setColorCode(String colorCode) {
        this.colorCode = colorCode;
    }

    public String getColorName() {
        return colorName;
    }

    public void setColorName(String colorName) {
        this.colorName = colorName;
    }

    public String getColorHex1() {
        return colorHex1;
    }

    public void setColorHex1(String colorHex1) {
        this.colorHex1 = colorHex1;
    }

    public String getColorHex2() {
        return colorHex2;
    }

    public void setColorHex2(String colorHex2) {
        this.colorHex2 = colorHex2;
    }

    public String getParentColor() {
        return parentColor;
    }

    public void setParentColor(String parentColor) {
        this.parentColor = parentColor;
    }
}

How can i handle it, if it returns NULL ? Thanks.

Upvotes: 3

Views: 7156

Answers (1)

Zun
Zun

Reputation: 1690

Your call getProductColorMotor() can throw an exception. Do the following thing if you want safe Java code:

1) Mark the variable and getter as nullable

@SerializedName("product_color_motor")
@Nullable
private ProductColorResponse productColorMotor;

@Nullable
public ProductColorResponse getProductColorMotor() {
        return productColorMotor;
}

2) Don't try to directly access getColorName(), instead, assign the productColorMotor to a variable.

ProductColorResponse p = dataList.get(position)

3) Now check if this is null and provide an alternative text to your txtColor

if (p == null) { holder.txtColor.setText("Alt text") } else { holder.txtColor.setText(p.getColorName())

Upvotes: 3

Related Questions