简体   繁体   中英

Why is retrofit response list null?

I'm developing an android app which uses Retrofit to make API call. I'm trying to fetch records from a table named "reception" with only one column rClose . The API call responds well in POSTMAN test. However, when I handle the response in my fragment code to get the response length (size) of the closeList the code runs with no error but the app crashes in run time and I get the following error:

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

Here is my code:

public class CustomerHomeFragment extends Fragment{

    private List<Close> closeList;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home_customer, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Call<CloseResponse> call = RetrofitClient.getInstance().getApi().getCloses();

        call.enqueue(new Callback<CloseResponse>() {
            @Override
            public void onResponse(Call<CloseResponse> call, Response<CloseResponse> response) {

                closeList = response.body().getCloses();


                int count = closeList.size();
                if(count < 1){
                    showFoodsInRecyclerView();
                }else{


                }

            }

            @Override
            public void onFailure(Call<CloseResponse> call, Throwable t) {

            }
        });



    }

}

Api.java

  @GET("allclose")
    Call<CloseResponse> getCloses();

Close.java

public class Close {

    private String rClose;

    public Close(String rClose) {
        this.rClose = rClose;

    }


    public String getRClose() {
        return rClose;
    }

}

CloseResponse.java

public class CloseResponse {

    private boolean error;
    private List<Close> closes;

    public CloseResponse(boolean error, List<Close> closes) {
        this.error = error;
        this.closes = closes;
    }

    public boolean isError() {
        return error;
    }

    public List<Close> getCloses() {
        return closes;
    }
}

Logcat:

2019-08-24 19:14:11.817 3718-3718/com.nikappzaar.app.foodtakeout E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nikappzaar.app.foodtakeout, PID: 3718
    java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at com.nikappzaar.app.foodtakeout.fragments.CustomerHomeFragment$1.onResponse(CustomerHomeFragment.java:77)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

The error is in the following code line:

int count = closeList.size();

I did some research on NullPointerException but I don't know why is the response list "null", while the same approach on other tables of my database is working well! What am I doing wrong?

Any response would be appreciated.

You need to initialize List with new in this fragment CustomerHomeFragment . Change this line private List closeList; with private List closeList = new ArrayList(); It will solve your error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM