如何使用改型处理JSON对象中的NULL值

时间:2018-12-11 09:12:53

标签: java android json retrofit retrofit2

我尝试使用这样的改造从json获取值

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

但是我遇到了这样的错误:

  

java.lang.String   com.qiscus.qismo.chat.data.model.ordermodel.ProductColorResponse.getColorName()'   在空对象引用上

我认为这是因为我从JSON响应中得到了空值:

...
      "product_color_motor": null
...

响应有时会返回NULL。

但是通常响应是这样的:

...
"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;
    }
}

如果返回NULL,该如何处理?谢谢。

1 个答案:

答案 0 :(得分:3)

您的呼叫getProductColorMotor()可能会引发异常。如果您想要安全的Java代码,请执行以下操作:

1)将变量和getter标记为可空

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

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

2)不要尝试直接访问getColorName(),而是将productColorMotor分配给变量。

ProductColorResponse p = dataList.get(position)

3)现在检查是否为空,并为您的txtColor

提供替代文本
if (p == null) { holder.txtColor.setText("Alt text") } else { holder.txtColor.setText(p.getColorName())