Home > Enterprise >  Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.removeViewAt(i
Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.removeViewAt(i

Time:09-27

Error:

     java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.removeViewAt (int)' on a null object reference
     W / System.err: at com.arvicapps.geomitric_wallpapers.FavoriteFullImageActivity $ 5.onFavoriteChanged (FavoriteFullImageActivity.java:533)
             at com.github.ivbaranov.mfb.MaterialFavoriteButton.updateFavoriteButton (MaterialFavoriteButton.java:324)
             at com.github.ivbaranov.mfb.MaterialFavoriteButton.setFavorite (MaterialFavoriteButton.java:257)
             at com.github.ivbaranov.mfb.MaterialFavoriteButton.toggleFavorite (MaterialFavoriteButton.java:292)
     W / System.err: at com.github.ivbaranov.mfb.MaterialFavoriteButton $ 1.onClick (MaterialFavoriteButton.java:152)
             at android.view.View.performClick (View.java:6659)
             at android.view.View.performClickInternal (View.java:6631)
             at android.view.View.access $ 3100 (View.java:790)
             at android.view.View $ PerformClick.run (View.java:26187)
             at android.os.Handler.handleCallback (Handler.java:907)

Here are parts of the code from FavoriteFullImageActivity.java:

@Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState);  setContentView (R.layout.activity_favorite_full_image);  getSupportActionBar (). hide ();  RecyclerView recyclerView = findViewById (R.id.FavoriteCategoryView);  }
 try {
  recyclerView.removeViewAt (position);  recyclerView.notifyAll ();
 myDbHelper.closeDb ();
 myDbHelper.close ();
 myDbHelper.notifyAll ();
 } catch (NullPointerException nullPointerException) {nullPointerException.printStackTrace ();
  }

How to solve?

CodePudding user response:

You're trying to access child view without inflating it or that child view does not exist. set adapter to recycler view

recyclerView.adapter=adapter

CodePudding user response:

java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.removeViewAt (int)' on a null object reference

It means that the property of recyclerView wasn't initiated before you use it. Regards this, you need to check your code carefully.

  • Related