android - NPE with retrofit and nested JSON response -


i using retrofit 1.9 , i'm having trouble getting values nested json response. here response

{   "access_token": "83ebeabdec09f6670863766f792ead24d61fe3f9",   "athlete": {     "id": 227615,     "resource_state": 3,     ...   } } 

i have class handle response:

public class authresponse {      string accesstoken;     stravaathlete stravaathlete;      public authresponse(string accesstoken, stravaathlete stravaathlete) {         this.accesstoken = accesstoken;         this.stravaathlete = stravaathlete;     }      public string getaccesstoken() {         return accesstoken;     }      public stravaathlete getstravaathlete() {         return stravaathlete;     } } 

and class nested json object:

public class stravaathlete {      string id;      public stravaathlete(string id) {         this.id = id;     }      public string getid() {         return id;     } } 

unfortunately whenever call authresponse.getstravaathlete().getid()

java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string com.drdp.rideart.api.model.stravaathlete.getid()' on null object reference 

not sure doing wrong. have retrofit logging set full , can verify information in response in format expect

accesstoken , stravaathlete not key of json. that's why getting null. can either change them access_token , athlete, or use @serializedname annotation. e.g.

public class authresponse {     @serializedname("access_token")    string accesstoken;    @serializedname("athlete")    stravaathlete stravaathlete; 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -