Reputation: 5758
I am trying to pass an Order Object between two activities. This object contains an ArrayList of MenuItems. The class for Order is below: (it implements the parcelable interface).
public class Order extends ArrayList <MenuItem> implements Parcelable {
private int covers;
private int table;
//To hold MenuItems
private ArrayList<MenuItem> items;
public Order(){
}
public static final Parcelable.Creator<Order> CREATOR = new Parcelable.Creator<Order>() {
@Override
public Order createFromParcel(Parcel source) {
return new Order(source);
}
@Override
public Order[] newArray(int size) {
return new Order[size];
}
};
public Order(int covers, int table, ArrayList<MenuItem> items) {
super();
this.covers = covers;
this.table = table;
this.items = items;
}
public int getCovers() {
return covers;
}
public void setCovers(int covers) {
this.covers = covers;
}
public int getTable() {
return table;
}
public void setTable(int table) {
this.table = table;
}
public ArrayList<MenuItem> getItems() {
return items;
}
public void setItems(ArrayList<MenuItem> items) {
this.items = items;
}
//add menu item to order
public void addToOrder(MenuItem m){
items.add(m);
}
//remove menu item from order
public void removeFromOrder(MenuItem m){
items.remove(m);
}
public Order(Parcel source) {
readFromParcel(source);
}
private void readFromParcel(Parcel source) {
this.clear();
//read the size of the list
int size = source.readInt();
//Remember order of items written into the Parcel. Important here.
for(int i = 0; i < size; i ++){
MenuItem item = new MenuItem();
item.setName(source.readString());
item.setPrice(source.readDouble());
this.add(item);
}
}
@Override
public void writeToParcel(Parcel dest, int arg1) {
int size = this.size();
dest.writeInt(size);
for(int i = 0; i < size; i ++){
MenuItem item = this.get(i);
dest.writeString(item.getName());
dest.writeDouble(item.getPrice());
}
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
}
When my activity starts I create a new order item and place it in an Intent to be sent to the next activity. When I click the button to start the new activity I get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.waitron5/com.example.waitron5.SectionsActivity}: java.lang.NullPointerException
Below is my code I use in MainActivity.java to place the object into the Intent.
//Order object to hold details about order for duration of session
Order order;
ArrayList<Order> orderList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//As soon as app is started up create an order object.
order = new Order();
orderList = new ArrayList<Order>();
orderList.add(order);
//reference to button and add listeners
orderBtn = (Button)findViewById(R.id.orderBtn);
orderBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Create a new intent and start up the sections activity...
i = new Intent(v.getContext(), SectionsActivity.class);
i.putExtra("data", (ArrayList<Order>)orderList);
startActivity(i);
}
});
}
Below is my code I use in SectionsActivity.java to retrieve my object from the Intent.
//Get order information
public void getOrderData(){
i = getIntent();
if(i != null && i.hasExtra("data")){
Toast.makeText(this.getApplicationContext(), "recieved", Toast.LENGTH_SHORT).show();
order = i.getParcelableExtra("data");
int size = order.size();
Toast.makeText(this.getApplicationContext(), String.valueOf(size), Toast.LENGTH_SHORT).show();
}
}
Does anyone know why I am getting this error. I know it's because It can't find the parcelable object, I just don't know how I should go about fixing it! Many thanks.
EDIT: I think problem lies somewhere within this chunk of code:
@Override
public void writeToParcel(Parcel dest, int flags) {
int size = this.size();
dest.writeInt(size);
for(int i = 0; i < size; i ++){
MenuItem item = this.get(i);
dest.writeString(item.getName());
dest.writeDouble(item.getPrice());
}
}
LOG CAT ERRORS
01-09 23:41:23.706: E/AndroidRuntime(22717): FATAL EXCEPTION: main 01-09 23:41:23.706: E/AndroidRuntime(22717): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.waitron5/com.example.waitron5.SectionsActivity}: java.lang.NullPointerException 01-09 23:41:23.706: E/AndroidRuntime(22717): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 01-09 23:41:23.706: E/AndroidRuntime(22717): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 01-09 23:41:23.706: E/AndroidRuntime(22717): at android.app.ActivityThread.access$600(ActivityThread.java:141) 01-09 23:41:23.706: E/AndroidRuntime(22717): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 01-09 23:41:23.706: E/AndroidRuntime(22717): at android.os.Handler.dispatchMessage(Handler.java:99) 01-09 23:41:23.706: E/AndroidRuntime(22717): at android.os.Looper.loop(Looper.java:137) 01-09 23:41:23.706: E/AndroidRuntime(22717): at android.app.ActivityThread.main(ActivityThread.java:5039) 01-09 23:41:23.706: E/AndroidRuntime(22717): at java.lang.reflect.Method.invokeNative(Native Method) 01-09 23:41:23.706: E/AndroidRuntime(22717): at java.lang.reflect.Method.invoke(Method.java:511) 01-09 23:41:23.706: E/AndroidRuntime(22717): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 01-09 23:41:23.706: E/AndroidRuntime(22717): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 01-09 23:41:23.706: E/AndroidRuntime(22717): at dalvik.system.NativeStart.main(Native Method) 01-09 23:41:23.706: E/AndroidRuntime(22717): Caused by: java.lang.NullPointerException 01-09 23:41:23.706: E/AndroidRuntime(22717): at com.example.waitron5.SectionsActivity.getOrderData(SectionsActivity.java:113) 01-09 23:41:23.706: E/AndroidRuntime(22717): at com.example.waitron5.SectionsActivity.onCreate(SectionsActivity.java:44) 01-09 23:41:23.706: E/AndroidRuntime(22717): at android.app.Activity.performCreate(Activity.java:5104) 01-09 23:41:23.706: E/AndroidRuntime(22717): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 01-09 23:41:23.706: E/AndroidRuntime(22717): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 01-09 23:41:23.706: E/AndroidRuntime(22717): ... 11 more
LOG CAT WARNING
*01-09 23:41:23.676: W/Bundle(22717): Key data expected Parcelable but value was a java.util.ArrayList. The default value was returned. 01-09 23:41:23.686: W/Bundle(22717): Attempt to cast generated internal exception: 01-09 23:41:23.686: W/Bundle(22717): java.lang.ClassCastException: java.util.ArrayList cannot be cast to android.os.Parcelable 01-09 23:41:23.686: W/Bundle(22717): at android.os.Bundle.getParcelable(Bundle.java:1171) 01-09 23:41:23.686: W/Bundle(22717): at android.content.Intent.getParcelableExtra(Intent.java:4330) 01-09 23:41:23.686: W/Bundle(22717): at com.example.waitron5.SectionsActivity.getOrderData(SectionsActivity.java:112) 01-09 23:41:23.686: W/Bundle(22717): at com.example.waitron5.SectionsActivity.onCreate(SectionsActivity.java:44) *
Upvotes: 1
Views: 8252
Reputation: 5758
I managed to resolve my problem. The issue was that the Order class was implementing Parcelable as it should. However because it contained a list of MenuItems the MenuItem class was also required to implement the Parcelable interface.
Once I did this the problem was fixed.
Upvotes: 3
Reputation: 7530
Change i = getIntent();
by
i = getIntent().getCharSequenceArrayListExtra("data");
Update to get ArrayList
Upvotes: 0
Reputation: 86948
It would be very helpful to see the full LogCat stack trace to know exactly where the error occurs, but I can see one potential problem right away.
Your default constructor doesn't call the super constructor:
public Order(){
}
So when writeToParcel()
is called you try to fetch values from what is probably a null list.
MenuItem item = this.get(i); // You never initialized the super class...
Upvotes: 2