Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
at com.Deevith1.samplestickerapp.StickerPackListActivity.showStickerPackList(StickerPackListActivity.java:60)
at com.Deevith1.samplestickerapp.StickerPackListActivity.onCreate(StickerPackListActivity.java:39)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2905)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3060)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1818)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
**Java code**
private void showStickerPackList(List stickerPackList) {
allStickerPacksListAdapter = new StickerPackListAdapter(stickerPackList, onAddButtonClickedListener);
packRecyclerView.setAdapter(allStickerPacksListAdapter);
packLayoutManager = new LinearLayoutManager((this));
packLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(
packRecyclerView.getContext(),
packLayoutManager.getOrientation()
);
packRecyclerView.addItemDecoration(dividerItemDecoration);
packRecyclerView.setLayoutManager(packLayoutManager);
packRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(this::recalculateColumnCount);
}
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference at com.Deevith1.samplestickerapp.StickerPackListActivity.showStickerPackList(StickerPackListActivity.java:60) at com.Deevith1.samplestickerapp.StickerPackListActivity.onCreate(StickerPackListActivity.java:39) at android.app.Activity.performCreate(Activity.java:7136) at android.app.Activity.performCreate(Activity.java:7127) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2905) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3060) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1818) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6762) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
private void showStickerPackList(List stickerPackList) {
allStickerPacksListAdapter = new StickerPackListAdapter(stickerPackList, onAddButtonClickedListener);
packRecyclerView.setAdapter(allStickerPacksListAdapter);
packLayoutManager = new LinearLayoutManager((this));
packLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(
packRecyclerView.getContext(),
packLayoutManager.getOrientation()
);
packRecyclerView.addItemDecoration(dividerItemDecoration);
packRecyclerView.setLayoutManager(packLayoutManager);
packRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(this::recalculateColumnCount);
}
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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=".StickerPackListActivity" tools:showIn="@layout/activity_sticker_pack_list">
</androidx.constraintlayout.widget.ConstraintLayout>