Adrian Le Roy Devezin
Adrian Le Roy Devezin

Reputation: 843

Getting null pointer with recycler adapter

my app is crashing due to a null pointer with the recyclerview adapter in the fragment code. I'm using this adapter to generate a card view list but cannot find out what exactly is causing this null pointer. The log has it happening when setting the adapter r.setAdapter(rA) in the faucet class.

Fragment code

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{

    v = inflater.inflate(R.layout.faucetcards, container, false);
    r = (RecyclerView) getActivity().findViewById(R.id.feedRecyclerView);

    rA = new RecyclerAdapter(generateCards());
    lm = new LinearLayoutManager(getActivity());
    r.setAdapter(rA);
    r.setLayoutManager(lm);

    return v;
}

Update:

My error is with r or setting the adapter. That's where the null pointer occurs even though rA is not null

Upvotes: 1

Views: 349

Answers (1)

Johannes
Johannes

Reputation: 223

The Nullpointer at r.set adapter(rA) occurs because r is null.

The reason for r being null is that you tried to find the RecyclerView in the Fragment's parent Activity. But the RecyclerView is contained in the Fragment's layout, which you inflated in the onCreateView method and stored in v. So what you should do instead is:

r = (RecyclerView) v.findViewById(R.id.feedRecyclerView);

Upvotes: 3

Related Questions