Creating a simple Java Response Class from a Json Response

  android, java, json, postman

I want to build a simple Response class for my nestetd and dynamic Json Response. I recently build my Response class with boolean error, String message and a List<User> user, but if i try to work with the List (for example i want to put out the size of the list or get a user from the list, the applikation returns an error)

Every time i try to get the response message with standardResponse.getMessage() it works, but when i try to open a user from the List, the applikation returns an error?

My dynamic json response:

{
    "error": false,
    "message": "API-Request successful",
    "user": {
        "0": {
            "userID": 11,
            "firstName": "David",
            "userName": "david",
            "birthDate": "2020-10-19",
            "gender": 1,
            "country": "Linz",
            "aboutmeText": "Testeintrag 3",
            "status": "I like Lookly."
        },
        "1": {
            "userID": 43,
            "firstName": "Sarah",
            "userName": "sarahh",
            "birthDate": "2001-12-14",
            "gender": 0,
            "country": "Austria",
            "aboutmeText": "Lookly ist eine hervorragende App!",
            "status": "Hey there! I am using Lookly."
        }
    }
}

My Java Response class:

public class StandardResponse {
    boolean error;
    String message;
    List<User> users;

    public boolean isError() {
        return error;
    }

    public String getMessage() {
        return message;
    }

    public List<User> getUsers() {
        return users;
    }
    
    public class User{
        private int userID;
        private String firstName;
        private String userName;
        private String birthDate;
        private String gender;
        private String country;
        private String aboutmeText;
        private String status;
    }
}

My Test Request Class in which i try to get back the size of the List<User>

public void updateListView(String latitude, String longitude) {
        Call<StandardResponse> call = RetrofitClient
                .getInstance()
                .getApi()
                .standard(
                        latitude,
                        longitude
                );

        call.enqueue(new Callback<StandardResponse>() {
            @Override
            public void onResponse(Call<StandardResponse> call, Response<StandardResponse> response) {
                StandardResponse standardResponse =  response.body();
                
                //Problem!!!
                if(response.isSuccessful()) {
                    int size = standardResponse.getUsers().size();
                    Log.d("Response message", standardResponse.getMessage());
                    Log.d("Size ", "" + size);
                }
                else  {
                    Log.d("Response errorBody", String.valueOf(response.errorBody()));
                }
            }

            @Override
            public void onFailure(Call<StandardResponse> call, Throwable t) {
                String message = t.getMessage();
                Log.e("onFailure", message);
            }
        });
    }

Problem:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: at.lookly.apitestapplikation, PID: 1056
    java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.indexOf(java.lang.Object)' on a null object reference
        at at.lookly.apitestapplikation.activities.TestRequest$2.onResponse(TestRequest.java:110)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0$DefaultCallAdapterFactory$ExecutorCallbackCall$1(DefaultCallAdapterFactory.java:89)
        at retrofit2.-$$Lambda$DefaultCallAdapterFactory$ExecutorCallbackCall$1$hVGjmafRi6VitDIrPNdoFizVAdk.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Source: Android Questions

LEAVE A COMMENT