Skip to content
Advertisement

How to pass a list of objects from one activity to another using parcelable

I want to pass the list of itemselected or ItemsInCart to another activity. My Items Model implements parcelable. The problem is am getting error below in my SecondActivity class.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference at com.example.Project1.SecondActivity.onCreate(SecondActivity.java:59)

Below is the code

Model Item;

JavaScript

}

First Activity; The list that i want to pass to second activity is ‘itemsInCart’

JavaScript

Second Activity (in OnCreate method):

JavaScript

I have found similar questions and tried their solutions but all is not working. Please advise on what i have to change.

Second Activity Adapter.

public class SecondActivity_Adapter extends RecyclerView.Adapter<SecondActivity_Adapter.MyViewHolder> { private ArrayList itemList;

JavaScript

}

Advertisement

Answer

Check whether you have added RecyclerView in xml i.e, activity_second.xml

If you have added Recyclerview in xml check whether you have referenced it using findViewById in SecondActivity

RecyclerView CartItemsInRecyclerView = findViewById(R.id.recyclerview_id)

You are getting error for Layout Manager i.e referencing it using null object reference , that means CartItemsInRecyclerView is null

Edit :

In First activity:-

JavaScript

In Second Activity:-

Instead of bundle.getgetParcelableArrayList try getIntent().getgetParcelableArrayList

JavaScript

Then check the result in Logcat

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement