1

I have an activity where I am adding objects into an ArrayList which implements the Parcelable interface. I then pass this list to another activity via a bundle however I get the following error when I try to print the size of the list in the new activity:

java.lang.RuntimeException: Unable to resume activity {com.example.test/com.example.test.SectionsActivity}: java.lang.NullPointerException

Here is the first activity where I add to list:

    @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Toast.makeText(getApplicationContext(), l.getItemAtPosition(position).toString() + " added to order", Toast.LENGTH_SHORT).show();
    // add clicked item to orderData....
    MenuItem m = (MenuItem) l.getItemAtPosition(position);
    // create new item
    orderData.add(m);
    subTotal += m.getPrice();
    calc();
}

This is first activity where I send the data via intent:

confirmBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            b.putParcelable("order", orderData);
            Intent i = new Intent(v.getContext(), SectionsActivity.class);
            i.putExtra("data",b);
            startActivity(i);

        }
    });

and this is second activity where I try to retrieve the bundle and display size:

@Override
protected void onResume() {
    super.onResume();
    getIntentData();
}


public void getIntentData(){
    Intent i = getIntent();
    if(i != null & i.hasExtra("data")){
        Toast.makeText(this.getApplicationContext(), "recieved", Toast.LENGTH_LONG).show();
        b = i.getExtras();
        orderData = b.getParcelable("order");
        int size = orderData.size();
        Toast.makeText(this.getApplicationContext(), String.valueOf(size), Toast.LENGTH_LONG).show();

    }
}

Any ideas why I am getting the null pointer?? It's driving me mad!

orderData is a MenuItemList object:

public class MenuItemList extends ArrayList <MenuItem> implements Parcelable{

/**
 * 
 */
private static final long serialVersionUID = 2998684930374219271L;



public MenuItemList(){

}

public static final Parcelable.Creator<MenuItemList> CREATOR = new Parcelable.Creator<MenuItemList>() {

    @Override
    public MenuItemList createFromParcel(Parcel source) {
        return new MenuItemList(source);
    }

    @Override
    public MenuItemList[] newArray(int size) {
        return new MenuItemList[size];
    }


};


public MenuItemList(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 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());

    }

}

@Override
public int describeContents() {
    return 0;
}

}

CHANGES TO CODE THAT SOLVED PROBLEM:

CHANGED onClick(View v):

@Override
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(), SectionsActivity.class);
            i.putExtra("data", (ArrayList<MenuItem>)orderData);
            startActivity(i);

        }

CHANGED getIntent():

public void getIntentData(){
    Intent i = getIntent();
    if(i != null && i.hasExtra("data")){
        Toast.makeText(this.getApplicationContext(), "recieved", Toast.LENGTH_LONG).show();
        orderData = i.getParcelableExtra("data");
        int size = orderData.size();
        Toast.makeText(this.getApplicationContext(), String.valueOf(size), Toast.LENGTH_LONG).show();

    }
}
Javacadabra
  • 5,578
  • 15
  • 84
  • 152
  • I am curious why this question got a -1? That is discouraging for new users who at least post relevant code, appropriate tags, and some logcat. Explained the problem clearly and showed effort...disappointing. +1 to you Javacadabra – codeMagic Dec 12 '12 at 01:04
  • @codeMagic Thank you for the supportive comment! – Javacadabra Dec 12 '12 at 01:46
  • You're welcome but that's what this site is about. I've asked worse questions when I first started, but thanks to the supportive SO community, I have learned a lot in the last 9 months or so. Don't let negative people get you down or keep you from asking questions as long as you show research effort. I'm glad you got your issue figured out. – codeMagic Dec 12 '12 at 01:49

2 Answers2

5

Rewrite
The problem is that you have an extra Bundle. Currently in getIntentData() you have to call:

getIntent()                     // Fetch the Intent, 
    .getExtras()                // Fetch the Bundle of extras, 
    .getBundle("data")          // Fetch the Bundle "data", 
    .getParcelable("order");    // Then get your parcelable...  

Let's cut out the unnecessary Bundle.

public void onClick(View v) {
    Intent i = new Intent(v.getContext(), SectionsActivity.class);
    i.putExtra("data", orderData);
    startActivity(i);
}

Now update getIntentData():

Intent i = getIntent();
if(i != null && i.hasExtra("data")){
    orderData = i.getParcelableExtra("data");
    ...
}
Sam
  • 86,580
  • 20
  • 181
  • 179
  • when I try to use the putParcelableArrayList() I get an error within the IDE saying that the arguments are not applicable. It's really frustrating because I can see exactly what you are saying I just don't know why my variables are not applicable!! – Javacadabra Dec 12 '12 at 01:17
  • Perhaps your title is misleading, how do you define `orderData`? What is it an ArrayList of? – Sam Dec 12 '12 at 01:18
  • I will include the class in the code above. It is instantiated from a class I named MenuListItem and that class extends Parcelable. – Javacadabra Dec 12 '12 at 01:21
  • I updated my answer. From your original question I though that the ArrayList type was Parcelable, but it is the ArrayList that is Parcelable. – Sam Dec 12 '12 at 01:30
  • Thank you very much, that helped big time! I had to change your answer slightly to make it work. I included the changes to my original question. – Javacadabra Dec 12 '12 at 01:42
4

Unless I missed something, you are using the bitwise & instead of the logical && in your getIntentData() function

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I just noticed that, I replaced the & with && but the result remains the same unfortunately, still a null pointer error. – Javacadabra Dec 12 '12 at 00:51
  • You don't seem to use the key "order" in your `putExtra`. You add it to whatever 'b' is but not your intent(i). Also, not your problem but you don't have to convert your int variable into a string to display in the text. You can just put your variable name `Toast.makeText(context, variable, length).show()` – codeMagic Dec 12 '12 at 00:56
  • @Sam thank you and one to you for explaining in good detail what I was thinking but didn't take the time to do – codeMagic Dec 12 '12 at 01:11