0

I am making a retrofit POST request using Paging library. When the first page is loaded the next page is automatically called. But when it comes to the last page (assume page 5), it requests for page 6, then on response the app crash with error

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

when it queries for page 6, from my LogCat, an HTTP response is OK with Content length 0. But when i do checking it crashes

        //...code
        private final ReplaySubject<Rating> ratingReplaySubject;

        //...code
        @Override
        public void onResponse(Call<ArrayList<Rating>> call, Response<ArrayList<Rating>> response) {
            if (response.isSuccessful()) {
                callback.onResult(response.body(), Integer.toString(page.get()+1));
                mutableLiveData.postValue(NetworkState.LOADED);

                //First i tried this,
                //response.body().forEach(ratingReplaySubject::onNext);

                //Then I tried this,
                //ArrayList<Rating> ratingArrayList = new ArrayList<>();
                //ratingArrayList = response.body();
                //if(ratingArrayList == null && !ratingArrayList.isEmpty()){
                //    for(int i=0; i<ratingArrayList.size(); i++){
                //        ratingReplaySubject.onNext(ratingArrayList.get(i));
                //    }
                //}

            } else {
                mutableLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message()));
                Log.e("API CALL", response.message());
            }
        }
Jasurbek
  • 2,946
  • 3
  • 20
  • 37
KIRPAL SINGH
  • 185
  • 2
  • 17
  • You're most likely attempting to call methods on an item that doesn't exist, hence a `NullPointerException`. Check if the page exists before getting it and performing operations on it. – Austin Schaefer Jun 14 '19 at 09:28
  • check list is empty or not before postValue. Where is your logcat? post it – M D Jun 14 '19 at 09:29
  • @AustinSchäfer I can do that, but what if my number of page is dynamic. Since the number is automatically incremented when last page is retrieved, i just want to check if results null dont do anything – KIRPAL SINGH Jun 14 '19 at 09:33
  • @MD I updated my question. How do i actually check if it is intantiated? – KIRPAL SINGH Jun 14 '19 at 09:47

0 Answers0