Get NullPointerException in android Studio with recycler

13,866
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

findViewById returns null here because you haven't yet called setContentView. All of that should be done in onCreate.

Edit:

There's no RecylerView with an id my_recycler_view in your layout.

Your tutorial told you to add it:

enter image description here

I'm not sure how you've missed it.

Share:
13,866
Shohom Shahd
Author by

Shohom Shahd

Updated on June 05, 2022

Comments

  • Shohom Shahd
    Shohom Shahd almost 2 years

    I'm new here. I followed a tutorial to create a card list using RecyclerView. The app crashes at launch and the log says:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wc.gap.worldcupfixture/com.wc.gap.worldcupfixture.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
    
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
     at com.wc.gap.worldcupfixture.MainActivity.onCreate(MainActivity.java:78)
    

    Pieces of code:

    //Variables
    private static RecyclerView.LayoutManager mLayoutManager;
    private static RecyclerView mRecyclerView;
    private static RecyclerView.Adapter mAdapter;
    private static ArrayList < Integer > removedItems;
    private static ArrayList < MatchData > matches;
    static View.OnClickListener myOnClickListener;
    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    
    //error here:
    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    
    //Line 78:
    mRecyclerView.setHasFixedSize(true);
    
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    

    I picked the code from THIS tutorial. The 2 arrays, 'matches' contains info about cricket matches and they are displayed as cards. When they are dismissed, they fill the 2nd array 'removedItems'. And the second array is used to add the removed match info back to the first array.

    When I removed the line, the same error was shown on line 80, then 81. The error persisted, but after deleting line 81 a specific line number was not shown in the error log. If it helps: the array matches is populated I have made a few adjustments to the tutorial's codes, but i cant pinpoint the problem. Exactly how can a boolean be null, or what does the error mean?

    Thanks in advance. I can post my entire code if needed.

    EDIT: more script:

    private static RecyclerView.LayoutManager mLayoutManager;
        private static RecyclerView mRecyclerView;
        private static RecyclerView.Adapter mAdapter;
        private static ArrayList<Integer> removedItems;
        private static ArrayList<MatchData> matches;
        static View.OnClickListener myOnClickListener;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            myOnClickListener = new MyOnClickListener(this);
            mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
            mRecyclerView.setHasFixedSize(true);
            mLayoutManager = new LinearLayoutManager(this);
            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    
            matches = new ArrayList<>();
            for (int i = 0; i < MyData.matchArray.length; i++){
                matches.add(new MatchData(
                        MyData.matchArray[i],
                        MyData.timingArray[i],
                        MyData.venueArray[i],
                        MyData.id_[i]
                ));
            }
    
            removedItems = new ArrayList<>();
    
            mAdapter = new MyAdapter(matches);
            mRecyclerView.setAdapter(mAdapter);
    
            //other things after
        }
    

    Activity_main.

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
        android:layout_width="match_parent" android:layout_height="match_parent"
        tools:context=".MainActivity">
    
    
        <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    
        <fragment android:id="@+id/navigation_drawer"
            android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
            android:layout_gravity="start"
            android:name="com.wc.gap.worldcupfixture.NavigationDrawerFragment"
            tools:layout="@layout/fragment_navigation_drawer" />
    
    </android.support.v4.widget.DrawerLayout>
    
  • Shohom Shahd
    Shohom Shahd over 9 years
    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myOnClickListener = new MyOnClickListener(this); mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); mRecyclerView.setHasFixedSize(true); mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.setItemAnimator(new DefaultItemAnimator());
  • Shohom Shahd
    Shohom Shahd over 9 years
    sorry for the disgusting format. i couldnt get it to display better btw, that is what i had. Im very new, please be more specific as to what i should edit
  • Simas
    Simas over 9 years
    @ShohomShahd post your activity_main.xml. Instead of commenting, update your question.
  • Shohom Shahd
    Shohom Shahd over 9 years
    I'm sure i typed it in and then deleted it when messing around with code.