0

I am trying to parse below JSON using GSON, but its always shows null Pointer Exception in this line if(myLifeExpVo.getLifeVo().getError().equals("false")){} I don't know why its happening. Any problem in my Getter Setter Classes i try to resolve this problem but same result.

I made separate model classes.

{
  "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"
        }
      ]
    }
  }
}  

And these are my Model classes 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;
    }
}

And Parser Method. In this method i mentioned where its show error.

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

please help me that how to resolve this problem.

Community
  • 1
  • 1
user3283148
  • 101
  • 3
  • 13
  • Just compare your JSON and your POJOs and check where there is a mismatch. Gson will leave a field null if there is no corresponding JSON field with the same name. – Sotirios Delimanolis Jan 03 '16 at 10:00
  • Thanks for reply but JSON fields is not the problem. i double check if you think that tell me change in shape of sample code – user3283148 Jan 03 '16 at 10:12
  • Where do you have anything called `lifeVo` in your JSON? – Sotirios Delimanolis Jan 03 '16 at 10:14
  • I basically add two classes in one single class. MyLifeVo and MyExpVo in to MyLifeExpVo.and lifeVo is class name as a variable to access the their specific objects. – user3283148 Jan 03 '16 at 10:17
  • Gson maps JSON to Java fields. What do you expect your `MyExpVo#lifeVo` to be mapped from? What is the corresponding piece of JSON? On that note, which part of the invocation is actually throwing the `NullPointerException`? That'll explain which part of your POJO hierarchy Gson couldn't deserialize. – Sotirios Delimanolis Jan 03 '16 at 10:21
  • in my json there are two parts actually myLife and MyExp so thats why i made sepearte model for each and put there params. so i expect that when i call MyExpVo or MyLifeVo from MyLifeExpVo like e.g MyLifeExpVo myLifeExpVo = null;myLifeExpVo.getLifeVo().getError().equals("false") its give error result to their corresponding model class. – user3283148 Jan 03 '16 at 10:33
  • If you have solution according to my JSON that how i parse using two model classes for each e.g MyLife and MyExp than change my code where i am doing mistake. – user3283148 Jan 03 '16 at 10:37

0 Answers0