D
D
dimasik1002002018-03-18 18:30:10
Java
dimasik100200, 2018-03-18 18:30:10

How to fix recycler view not using data error?

Created a recycler view with GridLayoutManager however it doesn't want to read data from prepareGalleryData(). Gives an error message

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference

And in the code this error points to two lines
at com.example.asus.testapp147.UserProfile_PhotoFragment.prepareGalleryData(UserProfile_PhotoFragment.java:58)

points to line 58 ( galleryList.add(gallery);) in
GalleryGridObject gallery = new GalleryGridObject(R.drawable.city);
        galleryList.add(gallery);

Here is the complete codeprepareGalleryData()
private void prepareGalleryData()
    {
        GalleryGridObject gallery = new GalleryGridObject(R.drawable.city);
        galleryList.add(gallery);

        gallery = new GalleryGridObject(R.drawable.city);
        galleryList.add(gallery);

        gallery = new GalleryGridObject(R.drawable.city);
        galleryList.add(gallery);

        mAdapter.notifyDataSetChanged();
    }

And on line 50 in the onCreateViewfragment
prepareGalleryData();
How to fix this error? (I'll throw off the full code of the fragment and adapter in the comments)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Yudakov, 2018-03-18
@dimasik100200

The galleryList field is not initialized anywhere, i.e. is always null.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question