android – setLayoutManager NullPointException in RecyclerView-ThrowExceptions

Exception or error:

I’m trying to receive a list with an array in RecyclerView and get error:

java.lang.NullPointerException: Attempt to invoke virtual method 
($LayoutManager)'on a null object reference

RecyclerView widget’s


I tried to declare LayoutManager to final, but it doesn’t help.

Activity code:

public class MainActivity extends ActionBarActivity {

private RecyclerView recyclerView;
private Toolbar toolbar;
private InfAdapter adapter;

protected void onCreate (Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.activity_main_appbar);

    recyclerView = (RecyclerView) findViewById(;

    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    InfAdapter mAdapter = new InfAdapter(this, getData());
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    toolbar = (Toolbar) findViewById(;


Adapter code:

public class InfAdapter extends RecyclerView.Adapter<InfAdapter.MyViewHolder> {

  private final Context context;
    private  List<Information> data = Collections.emptyList();

    public InfAdapter(Context context, List<Information> data){ = data;
        this.context = context;

    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_row, parent, false);
        return new MyViewHolder(v);

    public void onBindViewHolder(MyViewHolder  holder, int position) {

        Information current  = data.get(position);

    public int getItemCount() {
        return data.size();

    class MyViewHolder extends RecyclerView.ViewHolder{
    TextView title;
    ImageView icon;

    public MyViewHolder(View itemView) {

        title = (TextView) itemView.findViewById(;
        icon = (ImageView) itemView.findViewById(;


What has possibly gone wrong?

How to solve:

As @StenSoft noted in comments the problem was in
setContentView (R.layout.activity_main_appbar); because of wrong activity layout content, instead of
setContentView (R.layout.activity_main);


In my case this error comes because I did not create RecyclerView in one of my activity .

check your activity layout or fragment layout.

Leave a Reply

Your email address will not be published. Required fields are marked *