0

I am trying to pass a context in the the fragment onCreateVIew() method but I am getting the following 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

Here's my code

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.zoggfrombetelgeuse.closet.MySQL.mDownloader;



public class HomeFragment extends Fragment {

public HomeFragment() {
    // Required empty public constructor
}



static String urlAddress = "http://localhost/my-site/dbGet.php";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    final RecyclerView rv = (RecyclerView) view.findViewById(R.id.card_view);
    rv.setLayoutManager(new LinearLayoutManager(getActivity()));

    final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_layout);

    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            new mDownloader(HomeFragment.this.getActivity(), urlAddress, rv, swipeRefreshLayout).execute();
        }
    });

    return view;

}

}

Someone please explain this error in simple language and help me fix it. I have read other questions, I used onAttach() but didn't work

HomeFragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zoggfrombetelgeuse.closet.HomeFragment">

<!-- TODO: Update blank fragment layout -->
<android.support.v4.widget.SwipeRefreshLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/swipe_layout">
<android.support.v7.widget.RecyclerView
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/recycler_view"
    android:scrollbars="vertical">
    </android.support.v7.widget.RecyclerView>

</android.support.v4.widget.SwipeRefreshLayout>



</FrameLayout>
Happyfeet
  • 29
  • 5
  • Have you tried to debug this to find out what is null? I would guess `rv` might be null. – buczek Jul 07 '16 at 13:49
  • as in log `rv ` is null are u sure `card_view ` in `fragment_home .xml` file – ρяσѕρєя K Jul 07 '16 at 13:50
  • Sorry, I misread the question. It is likely your RecyclerView is not defined in your layout, or is defined with a different ID. findViewByID() will return null if it can't find the view. – Bobby StJacques Jul 07 '16 at 13:51
  • BTW, this is NOT an exact duplicate of the generic "null pointer exception" question and should not be marked as such. – Bobby StJacques Jul 07 '16 at 13:53
  • @BobbyStJacques I have checked everything. `card_view` is there and `RecyclerView` is also defined in the layout? Why do I need an id for `RecyclerView`? Does it have anything to do with `LayoutManager`? – Happyfeet Jul 07 '16 at 14:08
  • In order to use "findViewById" you need to provide the ID of the view you are trying to find. In this case, the view you are trying to find is the RecyclerView, so it needs an ID. Can you post your layout XML? – Bobby StJacques Jul 07 '16 at 18:59
  • @BobbyStJacques Just added the xml file. – Happyfeet Jul 07 '16 at 19:13
  • OK, so the ID you have given your recycler view is "recycler_view" but the argument you are supplying to the findViewById call is "R.id.card_view." There is nothing in the layout you shared with that ID, and so the method call will return null because that thing doesn't exist. Instead, you need to specify "R.id.recycler_view" as your argument to get the recycler view. – Bobby StJacques Jul 07 '16 at 19:28

0 Answers0