Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to perform pagination and have implemented it . It runs but the exact paages keep repeating themselves.

public class ScheduleDataSource extends PageKeyedDataSource<Long,GeneralMatchItem> {

public void loadInitial (@NonNull LoadInitialParams<Long> params, @NonNull LoadInitialCallback<Long, GeneralMatchItem> callback){


total_items = response_obj.getInt("total_items"); // This is the total items received fromthe response
....
....


callback.onResult(internationalScheduledMatchItemsList,null, 2L);



}



public void loadAfter(@NonNull LoadParams<Long> params, @NonNull LoadCallback<Long, GeneralMatchItem> callback) {


....

long nextKey = (params.key == total_items)? null : params.key+1; //  This throws null pointer when the pages are 12/12

callback.onResult(tempList,nextKey);

}




...
}


What I have tried:

I tried to track the position manually like this mentioned in the code block above:

As i keep srolling then the key gets added till it reaches 12,i.e, 12/12 complete and suddenly i get a null pointer exception

D/ScheduleDataSource: KEY 11AND TOTAL ITEMS :12


I was learning from this tutorial and had implemented most of it. But i can't get rid of this error .

The null pointer i get is :

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.xyz, PID: 5794
    java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
        at com.xyz.homescreen_new.utilities.schecdule_util.ScheduleDataSource$2.onResponse(ScheduleDataSource.java:252)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)






I am not able to understand what is going wrong in this. Please help .

TIA . :)
Posted
Updated 26-Apr-20 14:43pm
v4
Comments
David Crow 13-Jan-19 20:51pm    
Have you looked at line 252 of ScheduleDataSource.java?
Member 12731696 14-Jan-19 6:22am    
Hi David
Yes, at that line i am creatig a long value ,i.e , the key

long nextKey = (params.key == total_items)? null : params.key+1;
David Crow 14-Jan-19 15:49pm    
So have you checked to see if params is null?

Why are you assigning null to nextKey rather than something like 0 or -1?
Member 12731696 17-Jan-19 11:34am    
I was following this tutorial. I was trying to implement in that way..
https://proandroiddev.com/8-steps-to-implement-paging-library-in-android-d02500f7fffe
David Crow 17-Jan-19 11:56am    
So when nextkey equals null, longValue must be getting called from within onResult(), which is throwing the exception.

To verify, change that line of code to:
long nextKey = (params.key == total_items) ? 11 : params.key + 1;

1 solution

change long nextKey -> Long nextKey :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900