1

I was following this page Adding items to Endless Scroll RecyclerView with ProgressBar at bottom and I think I did everything right, but for some reason I get back on this line onLoadMoreListener.onLoadMore();

 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Handler.postDelayed(java.lang.Runnable, long)' on a null object reference

I know that there are a lot of posts regarding the null pointer error, but that line is in a if statement and it shouldn't be executed if it's null.

if (onLoadMoreListener != null) {
                        onLoadMoreListener.onLoadMore();
                    }

Why is this happening ? What code should I post to help figuring out the problem ?

mAdapter.setOnLoadMoreListener(new MyAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                //add progress item
                myDataset.add(null);
                mAdapter.notifyItemInserted(myDataset.size() - 1);

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //remove progress item
                        myDataset.remove(myDataset.size() - 1);
                        mAdapter.notifyItemRemoved(myDataset.size());
                        MyRecyclerItems gridItem= new MyRecyclerItems();
                        gridItem.setAbout("Test");
                        gridItem.setEmail("tesssst@gmail.com");
                        myDataset.add(0,gridItem);
                        mAdapter.notifyItemInserted(myDataset.size());
                        mAdapter.setLoaded();
                    }
                }, 2000);
                System.out.println("load");
            }
        });
Community
  • 1
  • 1
Bogdan Daniel
  • 2,249
  • 8
  • 32
  • 68

2 Answers2

8

This error means handler is null.

Just initialize handler object before calling post method like this:

handler=new Handler();
Saket Kumar
  • 438
  • 7
  • 17
2

As you pointed out the onLoadMoreListener field is not null based on the if statement. What you didn't include however is the code being executed inside the onLoadMore() method.

From the partial stack trace you are trying the call postDelayed(java.lang.Runnable, long) on a Handler inside the onLoadMore() method and said Handler has not been initialized (is null).

Cory Charlton
  • 8,432
  • 4
  • 46
  • 66
  • I posted the setOnLoadMoreListener. What do I have to initialize ? – Bogdan Daniel Nov 12 '15 at 18:47
  • You need to initialize the `handler` field variable. The SO post you linked is incomplete and a modification of another SO post (linked in the OP), http://stackoverflow.com/questions/27044449/put-an-indeterminate-progressbar-as-footer-in-a-recyclerview-grid/28090719#comment49412644_28090719 As you'll see in Vilen Melkumyan's answer they are initializing the `handler` variable in the `Activity.onCreate()` – Cory Charlton Nov 12 '15 at 18:55
  • I followed both of them but I have no idea how I missed that. Many thanks – Bogdan Daniel Nov 12 '15 at 19:00
  • am also fallowing this url https://github.com/kprathap23/Android/blob/master/EndlessRecyclerView/app/src/main/res/layout/wallpaper_activity.xml from there also sample only initially loading data only and then when i scroll up or down next hit not done.please help me appriciate it makes too late for me – Harsha Aug 23 '16 at 11:09