Justin Casey Justin Casey - 5 months ago 101
Android Question

Null object reference on LayoutManager

I am getting this error when trying to set my layoutmanager for my recyclerview. I have created a layout manager and set my recyclerview to use it. For some reason the layoumanager is null.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jcaseydev.popularmovies/com.jcaseydev.popularmovies.ui.ReviewsActivity}: 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
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2625)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2686)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5969)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
Caused by: 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
at com.jcaseydev.popularmovies.ui.ReviewsFragment.onCreateView(ReviewsFragment.java:65)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:619)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
at android.app.Activity.performStart(Activity.java:6686)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2588)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2686) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:5969) 
at java.lang.reflect.Method.invoke(Native Method)


Here is the recylerview class.

public ReviewsFragment(){}
RecyclerView mRecyclerView;
RecyclerView.LayoutManager linearLayoutManager;
RecyclerView.Adapter mAdapter;
private int movieId;
private List<Reviews> movieReviews = new ArrayList<>();


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

FetchMovieReviews fmr = new FetchMovieReviews();
// fmr.execute();

//get intent
Intent intent = getActivity().getIntent();

//key for the intent extra
String MOVIE_ID = "movie_id";
if(intent != null && intent.hasExtra(MOVIE_ID)){
movieId = intent.getIntExtra(MOVIE_ID, 49026);
}

mRecyclerView = (RecyclerView) rootView.findViewById(R.id.reviews_recyclerview);
linearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(linearLayoutManager);

movieReviews.add(0, new Reviews("test", "TESTING"));
movieReviews.add(1, new Reviews("test", "TESTING"));
movieReviews.add(2, new Reviews("tset", "TESTING"));

mAdapter = new MyAdapter(movieReviews);
mRecyclerView.setAdapter(mAdapter);
return rootView;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);



}

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
private List<Reviews> mDataset;

public class ViewHolder extends RecyclerView.ViewHolder{
public TextView mTextView;
public ViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView;
}
}

public MyAdapter(List<Reviews> reviews){
mDataset = reviews;
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.review_list_item, parent, false);

ViewHolder vh = new ViewHolder(view);
return vh;
}

@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
holder.mTextView.setText(mDataset.get(position).getAuthor());
}

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


I'm not sure what the problem could be.

Here is the review_fragment xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

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

</LinearLayout>


Here is the activity_review xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/review_activity"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>

Answer

The LayoutManager isn't null, your RecyclerView is. You're inflating R.layout.activity_review instead of R.layout.review_fragment.

Because of this, mRecyclerView = (RecyclerView) rootView.findViewById(R.id.reviews_recyclerview); results in mRecyclerView being null.

Thus, make sure you inflate R.layout.review_fragment:

View rootView = inflater.inflate(R.layout.review_fragment, container, false);
Comments