Martin Erlic
Martin Erlic

Reputation: 5667

RecyclerAdapter not loading data unless I refresh

This is how I initialize my adapter in my main FeedActivity:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
FeedAdapter adapter = new FeedAdapter(FeedActivity.this, retrieveYeets());
recyclerView.setAdapter(adapter);

This is my error:

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
    at com.yitter.feed.FeedAdapter.getItemCount(FeedAdapter.java:478)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3170)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3067)
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)
    at android.view.View.layout(View.java:16636)
    at android.view.ViewGroup.layout(ViewGroup.java:5437)
    at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:598)
    at android.view.View.layout(View.java:16636)
    at android.view.ViewGroup.layout(ViewGroup.java:5437)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:107

It's complaining about the following code in my FeedAdapter:

@Override
public int getItemCount() {
    return mYeets.size();
}

I have no clue why my list is empty. Obviously, there's a NullPointerException in my list somehow, but it's just not obvious. It might have to do with the positioning of the elements in my list. Anybody have suggestions?

Upvotes: 0

Views: 62

Answers (1)

Sahil Manchanda
Sahil Manchanda

Reputation: 10537

I think the problem lies here

public FeedAdapter(Context context, List<ParseObject> yeets) {
        super();

        mYeets = yeets;
        mContext = context;
        this.adapter = this;

        profilePictureImageLoader = new ImageLoader(new ProfilePictureFileCache(mContext));
    }

you forgot appending 'this' in the beginnig

public FeedAdapter(Context context, List<ParseObject> yeets) {
        super();

        this.mYeets = yeets;
        this.mContext = context;
        this.adapter = this;

        profilePictureImageLoader = new ImageLoader(new ProfilePictureFileCache(mContext));
    }

Upvotes: 1

Related Questions