1

I am trying to implement a activity which takes a list of object and passing to another activty for processing. But I get NPE. This is my code.

public class Order implements Parcelable { public static class OrderProduct { private String productId; private String qty; public OrderProduct(String productId, String qty) { this.productId = productId; this.qty = qty; } public String getProductId() { return productId; } public String getQty() { return qty; } public void setQty(String qty) { this.qty = qty; } @Override public String toString() { return "productId: " + productId + " qty: " + qty; } } private String customerId; private Map<String, OrderProduct> products; private OrderProduct product; public Order(String customerId) { this.customerId = customerId; products = new HashMap<String, OrderProduct>(); } public Order(Parcel in) { product.productId = in.readString(); product.qty = in.readString(); } public void setProduct(String productId, String qty) { product = products.get(productId); if (product == null) { product = new OrderProduct(productId, qty); } else { product.setQty(qty); } products.put(productId, product); } public Collection<OrderProduct> getProducts() { return products.values(); } public String getCustomerId() { return customerId; } @Override public String toString() { StringBuffer sb = new StringBuffer(); sb.append("Order: customerId: " + customerId + " { "); for (OrderProduct product : products.values()) { sb.append(" { ").append(product.toString()).append(" } "); } sb.append(" } "); return sb.toString(); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeString(product.getProductId()); dest.writeString(product.qty); } public static final Order.Creator<Order> CREATOR = new Order.Creator<Order>() { public Order createFromParcel(Parcel in) { return new Order(in); } public Order[] newArray(int size) { return new Order[size]; } }; } 
############################# ACTIVITY 1 ##################
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.send_menu: //saveOrder(order); showOrderReview(); break; } return true; } 
############## ACITIVITY 2
public class ReviewOrderActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); Intent i = getIntent(); Order myOrderObject = (Order) i.getParcelableExtra("order"); Collection<Order.OrderProduct> products = myOrderObject.getProducts(); System.out.println("Orderrrrrrrrrrrr : " + products.size()); for (Order.OrderProduct product : products){ Log.d("---", " product.getQty() "+product.getQty()+" "+product.getProductId()); } 

}

2
  • 2
    post the NPE call stack and also point it in the code Commented Aug 6, 2012 at 6:17
  • FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pda.kaizen/com.pda.kaizen.activity.ReviewOrderActivity}: java.lang.NullPointerException at com.pda.model.Order$OrderProduct.access$0(Order.java:13) at com.pda.model.Order.<init>(Order.java:51) at com.pda.model.Order$1.createFromParcel(Order.java:102) at com.pda.model.Order$1.createFromParcel(Order.java:1) at android.os.Parcel.readParcelable(Parcel.java:2103 at android.os.Parcel.readValue(Parcel.java:1965 at android.os.Parcel.readMapInternal(Parcel.java:2226 at android.os.Bundle.unparcel(Bundle.java:223 Commented Aug 6, 2012 at 7:20

1 Answer 1

1

Without seeing your LogCat, this is the best that stands out:

public Order(Parcel in) { product.productId = in.readString(); product.qty = in.readString(); } 

When this constructor is called, product is null. Additionally, in this constructor, products is not defined. Instead, do:

public Order(Parcel in) { String productId = in.readString(); String qty = in.readString(); product = new OrderProduct(productId, qty); products = new HashMap<String, OrderProduct>(); } 
Sign up to request clarification or add additional context in comments.

9 Comments

07-24 13:49:44.154: E/AndroidRuntime(1851): FATAL EXCEPTION: main 07-24 13:49:44.154: E/AndroidRuntime(1851): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pda.kaizen/com.pda.kaizen.activity.ReviewOrderActivity}: java.lang.NullPointerException 07-24 13:49:44.154: E/AndroidRuntime(1851): at com.pda.model.Order$1.createFromParcel(Order.java:102) 07-24 13:49:44.154: E/AndroidRuntime(1851): at com.pda.model.Order$1.createFromParcel(Order.java:1)
you answer was helpful to get rid of the error but in the ReviewOrder activity i get the list size 0 even though i had one order in it.
Are you sure setProduct is called?
thanks for your feedback. Can you please elaborate more on your comment
The Parcel constructor is only called by the Parcelable interface and shouldn't be called by you. For the reason your products list is empty in Activity2, it's because that list is not parceled. If you need further guidance on this, start a new question with your code and detailed problem; exchanging code by comment is impossible.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.