-3

Can you see the problem in my code? I get this exception:

java.lang.NullPointerException:  
Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

With this code:

RecyclerView recyclerView;
RecyclerView.Adapter adapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.latest_events_fragment, container, false);

    recyclerView = (RecyclerView)view.findViewById(R.id.events_recycler);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(linearLayoutManager );
    list.add("A");
    list.add("B");
    list.add("C");;
    // specify an adapter
    adapter = new EventsRecyclerAdapter(list);
    recyclerView.setAdapter(adapter);


    return view;
}

here is my .xml file it's called events_list.xml `

`

Rouvé
  • 415
  • 5
  • 4

1 Answers1

1

Your recycler view object is null. Make sure you are using correct id when trying to find it here:

recyclerView = (RecyclerView)view.findViewById(R.id.events_recycler);

If the id is correct then make sure you are using the layout you want.

Gennadii Saprykin
  • 4,505
  • 8
  • 31
  • 41