24

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

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

RecyclerView widget's

   <android.support.v7.widget.RecyclerView
        android:id="@+id/drawerList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>

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;


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

    recyclerView = (RecyclerView) findViewById(R.id.drawerList);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));

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

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

}

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){
        this.data = data;
        this.context = context;
    }

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


    @Override
    public void onBindViewHolder(MyViewHolder  holder, int position) {

        Information current  = data.get(position);
        holder.title.setText(current.title);
        holder.icon.setImageResource(current.iconid);
    }


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

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

    public MyViewHolder(View itemView) {

        super(itemView);
        title = (TextView) itemView.findViewById(R.id.listText);
        icon = (ImageView) itemView.findViewById(R.id.listIcon);

        }
    }
}

What has possibly gone wrong?

Ardi
  • 1,811
  • 4
  • 17
  • 29
  • 1
    can you post your layout code too....does your layout code have RecyclerView like this ` – Psypher Feb 17 '15 at 21:11
  • 1
    @Ranjith yes, thats included – Ardi Feb 17 '15 at 21:59
  • 1
    The LogCat output should provide you with the specific line of code where the null pointer exception is occurring or you can run this in the debugger and put a break point at the setLayoutManger line of code and see which object is null. – faljbour Feb 18 '15 at 00:08
  • Provide your entire layout file. This looks like the layout you set for the activity does not contain the id used for the recycler view. – Larry Schiefer Feb 18 '15 at 00:20
  • 7
    Is the `RecyclerView` in `layout/activity_main_appbar.xml`? – StenSoft Feb 18 '15 at 00:35

4 Answers4

43

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);

Ardi
  • 1,811
  • 4
  • 17
  • 29
10

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

check your activity layout or fragment layout.

thor
  • 21,418
  • 31
  • 87
  • 173
0

I had something similar ,but in kotlin and inside the

RecyclerView.apply {} 

code, in the line :

layoutManager = viewManager

eventually I found out it was because I inflated the wrong layout to the fragment, in the line:

return inflater.inflate(R.layout.main_fragment, container, false)
Gilad Levinson
  • 234
  • 2
  • 12
-1

seriously, for the record, check the recicler view id

  • idea given by https://stackoverflow.com/a/42751385/21396068 – Fiendel Mar 14 '23 at 11:59
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 19 '23 at 08:09
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34032216) – Sergei Kozelko Mar 21 '23 at 01:41