NullPointerException - Attempt to invoke virtual method RecyclerView$ViewHolder.shouldIgnore()' on a null object reference

recyclerview null pointer exception in fragment
recyclerview selectiontracker example
recyclerview null object reference
android-recyclerview focus item
recyclerview arrayadapter
how to get selected item from recyclerview in android
recyclerview long press select
recyclerview kotlin

Several developers have reported seeing the following stack trace since upgrading to Android Support 23.2.0:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2913)
    at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1445)
    at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:144)
    at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:282)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:821)
    at android.view.Choreographer.doCallbacks(Choreographer.java:606)
    at android.view.Choreographer.doFrame(Choreographer.java:575)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:807)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6895)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

This occurs when RecyclerView's change animation is enabled and corresponding RecyclerView.Adapter methods notifyItemInserted(), notifyItemRemoved(), etc., are called to indicate that an isolated change was made to the list managed by the adapter (as opposed to a wholesale change, as indicated by notifyDataSetChanged()).

Is this due to a bug in RecyclerView, or are we developers doing something wrong?

In my case the error caused because I was setting a new RecyclerView.LayoutParams into the rootview of an item.

Then I realized that RecyclerView item views actually store their ViewHolders in a custom LayoutParams class. So when I reset the LayoutParams ViewHolder reference is gone forever. Which causes a NullPointerException crash later.

The problem is gone after I stopped setting the RecyclerView.LayoutParams into the item rootView. :)

So. Stop doing that in your ViewHolder:

//DON'T DO THAT
RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
itemRoot.setLayoutParams(params);

NullPointerException in RecyclerView � Issue #585 � airbnb/epoxy , NullPointerException: Attempt to invoke virtual method 'boolean androidx. recyclerview.widget.RecyclerView$ViewHolder.shouldIgnore()' on a� Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

If you absolutely need to modify layout params, you can use the default item layout params like this:

    ViewGroup.LayoutParams params = itemView.getLayoutParams();
    params.height=xx;
    params.width= xx;
    params.yyyy = xxx;
    itemView.setLayoutParams(params);

java.lang.NullPointerException: Attempt to invoke virtual , NullPointerException: Attempt to invoke virtual method 'void android.view.View. setAdapter(adapter); //has to be called AFTER RecyclerView.setAdapter() OnClickListener() { @Override public void onClick(View view) { if (num1.getText() � java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference

This appears to be due to a bug in RecyclerView, which was introduced in 23.2.0. The bug was reported here, and I explained what I think is causing the error in comment #5 on that bug.

Here's my explanation, copied here for historical purposes and ease of reference:

I found the source of this problem. Within RecyclerView.dispatchLayoutStep3(), there's a for loop, "for (int i = 0; i < count; ++i)", where count is based on mChildHelper.getChildCount(). While this iteration is occurring, the collection managed by ChildHelper is modified by ChildHelper.hideViewInternal(), which results in null being returned from the call to mChildHelper.getChildAt() on line 3050 of RecyclerView, which in turn results in null being returned from getChildViewHolderInt() on the same line of code (RecyclerView:3050).

Here's the chain of method calls that results in the modification that breaks the integrity of the for loop:

dispatchLayoutStep3() -> animateChange() -> addAnimatingView() -> hide() -> hideViewInternal()

When ChildHelper adds the child param to its mHiddenViews collection, it violates the integrity of the for loop way up in dispatchLayoutStep3().

I see two workarounds for this:

1) Disable change animation in your RecyclerView

2) Downgrade to 23.1.1, where this wasn't a problem

Attempt to invoke virtual method 'boolean androidx.recyclerview , NullPointerException: Attempt to invoke virtual method 'boolean android.support. v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference Hi! I&#39;ve the following stack trace some times whene calling notifyParentItemChanged(i) java.lang.NullPointerException: Attempt to invoke virtual method &#39;boolean android.support.v7.widget.Re

I met up with this exception just now, and I fixed it by changing framgent to FragmentLayout.

My adapter used some data in fragment argument, and the using fragment in xml does not fill the data, so the bug occurs.

Just post this here, maybe useful to someone.

RecycleView usage error causes RecyclerView$ViewHolder , NullPointerExceptionAttempt to invoke virtual method 'boolean android.support. v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference. Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

I also encountered into this issue, even with version of RecyclerView 27.1.1. And I had the following code in my project:

    recyclerView.setLayoutManager(mLayoutManager);
    SimpleItemAnimator itemAnimator = new DefaultItemAnimator();
    itemAnimator.setSupportsChangeAnimations(false);
    recyclerView.setItemAnimator(itemAnimator);
    LayoutInflater inflater = LayoutInflater.from(getContext());

And I've fixed it after removing addition of Animator to RecyclerView, i.e. this code started to look the following way:

    recyclerView.setLayoutManager(mLayoutManager);
    LayoutInflater inflater = LayoutInflater.from(getContext());

java.lang.NullPointerException: Attempt to invoke virtual , Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView. NullPointerException на ViewPager с Recyclerview В нашем приложении есть ViewPager с FragmentPagerAdapter который содержит три фрагмента. Два из этих фрагментов состоят из Recyclerview для каждого из них.

NullPointerException for setLayoutManager, even if I updated build , java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object� NullPointerException en ViewPager con Recyclerview. Tenemos en nuestra aplicación un ViewPager con un FragmentPagerAdapter que contiene tres fragmentos. Dos de estos fragmentos se componen con una Recyclerview para cada uno. La primera página (el fragmento sin ViewPager) se muestra correctamente.

android试图调用虚方法recyclerview$viewholder shouldignore ( ) 在 , NullPointerException: Attempt to invoke virtual method 'boolean android.support. v7.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null�

在带有回收视图的ViewPager上抛出NullPointerException, That boolean will tell the ViewHolder if the item in that position has been selected by the createSelectAnything() allows us to select multiple items from our list. NullPointerException: Attempt to invoke virtual method 'int�

Comments
  • The reported issue has been fixed in future support library. If you come across the issue again in future support libraries, please let us know by raising a new issue with all necessary information including a sample application. If any issue persists, please report at Google issue tracker they will re-open to examine. issuetracker.google.com/issues/37078411
  • I could reproduce the same error with the library androidx.recyclerview:recyclerview:1.0.0 library. I inspected the data passed to RecyclerViewAdapter and there were duplicate IDs. Modified getItemId(position: Int) to produce unique IDs and it was fixed.
  • What's the alternative solution to hide gaps when setting visibility of the item to GONE?
  • Actually I would expect the item view to be disappear rather than leaving a gap, but since you asked maybe you can try this : this.itemView.getLayoutParams().height=0; or force the item to be redrawn : itemView.requestLayout();
  • Thanks for information, this bug seems to be fixed in 23.2.1 (at least it works for me now)
  • I am facing this issue in 24.2.1 version. I am trying to add scrollerview programmatically.
  • 25.0.0 is reprodused this error with setHasStableIds(true)
  • Issue is still there in 25.1.1 version.
  • @markproxy, I downgraded my api to 23.1.1 but still getting the issue. One year has passed. Is there any work around at the moment?