0

I am currently making a get request using volley and in onResponse i am trying to parse the jsonObject using gson to my model.

JSON Returned after the request is made:

{
   "success": 1,
   "message": "Done",
   "data": {
             "company": "companyname",
             "email": "email",
             "color": "#ffffff",
             "text_color": "#000000",
             "background_image": "BITMAP ENCODED",
             "logo": "BITMAP ENCODED",
             "enable_health_and_safety": 0,
             "health_and_safety": "",
             "sign_in_button_text": "SIGN IN",
             "sign_out_button_text": "SIGN OUT",
             "enable_picture": 0,
             "package_type": "TRIAL",
             "created_at": "2017-03-06 12:21:23",
             "updated_at": "2017-03-06 12:21:23",
             "telephone": "",
             "auto_sign_out": 0,
             "sign_out_time": "",
             "enable_printing": 0
          }
}

My Model

public class ClientResponseModel implements Serializable {
private String success;
private String message;
private ClientData clientData;

public String getSuccess() {
    return success;
}

public void setSuccess(String success) {
    this.success = success;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public ClientData getClientData() {
    return clientData;
}

public void setClientData(ClientData clientData) {
    this.clientData = clientData;
}

public class ClientData implements Serializable {
    private String company;
    private String email;
    private String client_color;
    private String text_color;
    private String background_image;
    private String logo;
    private int enable_health_and_safety;
    private String health_and_safety;
    private String sign_in_button_text;
    private String sign_out_button_text;
    private int enable_picture;
    private String package_type;
    private String created_at;
    private String updated_at;
    private String telephone;
    private int auto_sign_out;
    private String sign_out_time;
    private int enable_printing;

    //all the getters and setters for above 
}

My onResponse function

final JsonObjectRequest getClientData = new JsonObjectRequest(Request.Method.GET, clientUrl, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.i("getUserDataOnResponse", response.toString());
            Gson gson = new Gson();
            ClientResponseModel clientResponseModel = gson.fromJson(response.toString(), ClientResponseModel.class);


            Log.i("sucess",clientResponseModel.getSuccess());
            Log.i("message",clientResponseModel.getMessage());
            Log.i("company",clientResponseModel.getClientData().getEmail());

        }

I can get the success and message however when trying to get the data from clientData, Forexample getting the email. The app crashes and i get a following error message.

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String ClientResponseModel$ClientData.getEmail()' on a null object reference

I am not sure what i could be doing wrong, i have had looked at few links for example How to use gson and volley parse nested json

Thanks for all the help in advance.

5
  • JSONObject response then gson.fromJson(response.toString()... parsing json twice ... what a waste of resources Commented Mar 7, 2017 at 15:52
  • 1
    Thank you for your comment, firstly i am learner and learn by mistake so i take your and will make my code better, secondly the comment left could be left with helping spirit not an critic, and if left as critic please leave an answer as the way you would have done it, so at least other people can learn. @Selvin Commented Mar 7, 2017 at 16:19
  • try @SerializedName("data") @Expose private ClientData clientData; otherwise you will have to use annotation for every field Commented Mar 7, 2017 at 16:30
  • Hi @PavneetSingh Thanks for your comment, as mentioned earlier, i am just learning. Just for my Knowledge could you please explain how i can implement it. where i would implement the Expose private ClientData clientData;? Am i correct in saying SerializeName("data") goes above private ClientData clientdata; and Expose statement would go above the ClientData class? Commented Mar 7, 2017 at 18:53
  • \n mean enter @SerializedName("data") \n@Expose\n private ClientData clientData; , i am not sure that it will work or not Commented Mar 7, 2017 at 18:56

3 Answers 3

1

There is a little mistake in your code.So you should change ClientData clientData to ClientData data. Then you can get the data.Hope this will help you in parsing.

Sign up to request clarification or add additional context in comments.

Comments

1

i have a really easy approach for you:

first of all create the main model: and the other one for "data"

public class FeedBackModel {
    int success;
    String message;
    DataModel data;

}

public class DataModel {

    String company;
    String email;
    //etc ...
}

// create getter& setter for variables

then when you got json, parse it like this:

            Gson gson = new Gson();
            Type CollectionType = new TypeToken<FeedBackModel>() {
            }.getType();
            FeedBackModel FeedBack = gson.fromJson(json, CollectionType);

Comments

1

As Mentioned by user1912026, change as below.

private ClientData data;

And change the setter and getter as below

public ClientData getClientData() {
    return data;
}

public void setClientData(ClientData clientData) {
    this.data = clientData;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.