使用GSON进行配对时出现NullPointer异常

时间:2016-01-03 09:58:55

标签: java json eclipse android gson

我正在尝试使用GSON解析JSON以下,但是如果(myLifeExpVo.getLifeVo()。getError()。equals(" false")){},它总是在此行中显示空指针异常。 我不知道为什么会这样。我的Getter Setter类中的任何问题我尝试解决此问题,但结果相同。

我制作了单独的模型类。

{
  "mylife": {
    "error": false,
    "year": "2016",
    "month": "01",
    "userid": 57,
    "no_of_weeks": 5,
    "weeks": {
      "w1": [
        {
          "img_id": "334",
          "imagepath": "http://lh3.googleusercontent.com/QQKXPWm5Yiy6mGr2f828sKlog8shWxvTLgfLE1bPVYzXrO6mKrcEIl_ghNnN8nIMDf1O_PJPlfVoY1g",
          "caption": "",
          "uploadedTime": "02/Jan/2016"
        },
        {
          "img_id": "333",
          "imagepath": "http://lh3.googleusercontent.com/IwZ2GACYxUq2wi4zPYD53bHRfig7PKmORcxEG12coBEcq9pS-YaeV0F58ScwVZ17uLv0eyB2skc",
          "caption": "",
          "uploadedTime": "02/Jan/2016"
        },
        {
          "img_id": "140",
          "imagepath": "http://lh3.googleusercontent.com/s-biEJKu-GUTxqqi20aEQ-JMniSJPxr_Iw_3O20rmwhc-mudQlP9bV9QhpQST9EGTTtQ",
          "caption": "",
          "uploadedTime": "02/Jan/2016"
        },
        {
          "img_id": "139",
          "imagepath": "http://lh3.googleusercontent.com/cSfKliDo7-zWPuuUHNZjfoXtcCzxfrMXn-LP6TgEA4KA_JASS7u-Wpi8LUc",
          "caption": "",
          "uploadedTime": "02/Jan/2016"
        },
        {
          "img_id": "138",
          "imagepath": "http://lh3.googleusercontent.com/WGW71GgonEszsQZU2orE-y84u_5hoXnRMPmTRMEGA1njPUEZGMRjIEFQcdCPo",
          "caption": "",
          "uploadedTime": "02/Jan/2016"
        }
      ]
    }
  },
  "myExperiences": {
    "error": false,
    "year": "2016",
    "month": "01",
    "userid": 57,
    "no_of_exp": 5,
    "experiences": {
      "e2": [
        {
          "img_id": "4",
          "imagepath": "http://lh3.googleusercontent.com/2CwXPancHfP_SzuO-Qg4DWlHFJqAYmY6kvbEj4yS6Z_dOs5cQBjbSYOLpK4GQt39IAlVgCJa12StiPovdceX_9RZ",
          "caption": "",
          "uploadedTime": "03/Jan/2016"
        },
        {
          "img_id": "3",
          "imagepath": "http://lh3.googleusercontent.com/TsBbO0bmJmunC1azmQbU4pSE2zIbLkCd-jhgcp7FzeZ5yx01IMXAz8c",
          "caption": "",
          "uploadedTime": "03/Jan/2016"
        }
      ],
      "e1": [
        {
          "img_id": "2",
          "imagepath": "http://lh3.googleusercontent.com/YER6qq3RcOLGSN04xPs2Rj-Tp16JpLF6JGSSrEu5up1XeOP0AfqJwSvV",
          "caption": "",
          "uploadedTime": "03/Jan/2016"
        },
        {
          "img_id": "1",
          "imagepath": "http://lh3.googleusercontent.com/w-AKxfI5VTQGZrg3eHd9VRhXQ2nxnmcu3DJJA",
          "caption": "",
          "uploadedTime": "03/Jan/2016"
        }
      ]
    }
  }
}  

这些是我的Model类 MyLifeVo .java

public class MyLifeVo implements Serializable {
    private String error;
    private String year;
    private String month;
    private String userid;
    private String message;
    private String no_of_weeks;         
    public Weeks weeks;

  public static class Weeks {  
    @SerializedName("w1")
    private ArrayList<w1> w1;

    public ArrayList<w1> getW1() {
        return w1;
    }
    public void setW1(ArrayList<w1> w1) {
        this.w1 = w1;
    }
}
      public Weeks getWeeks() {
        return weeks;
    }
      public void setWeeks(Weeks weeks) {
        this.weeks = weeks;
    }    
    public String getYear() {
        return year;
    }
    public void setYear(String year) {
        this.year = year;
    }
    public String getMonth() {
        return month;
    }
    public void setMonth(String month) {
        this.month = month;
    }
    public String getUserid() {
        return userid;
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }
    public String getError() {
        return error;
    }
    public void setError(String error) {
        this.error = error;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getNo_of_weeks() {
        return no_of_weeks;
    }
    public void setNo_of_weeks(String no_of_weeks) {
        this.no_of_weeks = no_of_weeks;
    }
}

MyExpVo.java

public class MyExpVo implements Serializable {
    private String error;
    private String year;
    private String month;
    private String userid;
    private String message;
    private String no_of_exp;   
    public Experiences experiences;

   public static class Experiences {
    @SerializedName("e1")
    private ArrayList<e1> e1;
    public ArrayList<e1> gete1() {
        return e1;
    }
    public void sete1(ArrayList<e1> e1) {
        this.e1 = e1;
    }
}
    public String getYear() {
        return year;
    }
    public void setYear(String year) {
        this.year = year;
    }
    public String getMonth() {
        return month;
    }
    public void setMonth(String month) {
        this.month = month;
    }
    public String getUserid() {
        return userid;
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }
    public String getError() {
        return error;
    }
    public void setError(String error) {
        this.error = error;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getNo_of_exp() {
        return no_of_exp;
    }
    public void setNo_of_exp(String no_of_exp) {
        this.no_of_exp = no_of_exp;
    }
    public Experiences getExperiences() {
        return experiences;
    }
    public void setExperiences(Experiences experiences) {
        this.experiences = experiences;
    }
}

MyLifeExpVo.java

public class MyLifeExpVo implements Serializable {
    private MyLifeVo lifeVo;    
    private MyExpVo expVo;
    public MyLifeExpVo() {
        // TODO Auto-generated constructor stub
    }
     public MyLifeExpVo(MyLifeVo myLife, MyExpVo myExpVo) {
            this.lifeVo = myLife;
            this.expVo = myExpVo;
        }
    public MyLifeVo getLifeVo() {
        return lifeVo;
    }
    public void setLifeVo(MyLifeVo lifeVo) {
        this.lifeVo = lifeVo;
    }
    public MyExpVo getExpVo() {
        return expVo;
    }    
    public void setExpVo(MyExpVo expVo) {
        this.expVo = expVo;
    }
}

和解析器方法。在这个方法中我提到了它的显示错误。

public static MyLifeExpVo parseSearchExpLifeData(String response) {
            MyLifeExpVo myLifeExpVo = null;
            Gson gson;
            try {
                JSONObject jsonObject = new JSONObject(response);
                if(jsonObject !=null && jsonObject.has(KEY_MYLIFE)){
                    JSONObject jsonObject2 = jsonObject.getJSONObject(KEY_MYLIFE);
                    gson = new GsonBuilder().create();
                    myLifeExpVo = gson.fromJson(jsonObject2.toString(), MyLifeExpVo.class);
                    if(myLifeExpVo.getLifeVo().getError().equals("false")){//null pointer
                    }else { 
                    }
                }

            }if(jsonObject !=null && jsonObject.has(KEY_MYEXP)){
                JSONObject jsonObject3 = jsonObject.getJSONObject(KEY_MYEXP);
                gson = new GsonBuilder().create();
                myLifeExpVo = gson.fromJson(jsonObject3.toString(), MyLifeExpVo.class);
                if(myLifeExpVo.getExpVo().getError().equals("false")){
                    if(!myLifeExpVo.getExpVo().getExperiences().equals("")){                        
                }else {

                }
            }
        }       
    } catch (Exception e) {
        e.printStackTrace();
        Log.d("JSON Exception", e.toString());
    }
    return myLifeExpVo;}}

请帮我解决这个问题。

0 个答案:

没有答案