1

I am badly stuck to pass object from one activity to another activity using Parcelable but I am getting null pointer exception at line Log.i("Name",""+rcp.getName());, you can check this line in below code. Plz do check code CookingDataModel class at the end.

Here is the code of object Receiving Activity

protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// No title to display
        setContentView(R.layout.recipe_ingredient_detail);

        CookingDataModel cook = new CookingDataModel();
        RecipeDataModel rcp;
        ArrayList<IngredientDataModel> ing;

        Bundle bundle  = this.getIntent().getExtras();
        if(bundle!=null)
        cook = bundle.getParcelable("Cooking");

        rcp = cook.getRecipe();
        ing = cook.getIngredientList();


        Log.i("Name",""+rcp.getName());
        Log.i("Decrp",""+rcp.getDescription());
        Log.i("Duration",""+rcp.getPrepTime());
        Log.i("Instructions",""+rcp.getInstructions());

        for(int k = 0; k < ing.size(); k++)
        {
            Log.i("Item Name",""+ing.get(k).getItemName());
            Log.i("Item Amount",""+ing.get(k).getItemAmount());
        }
    }

Here is the code where I am sending object of CookingDataModel .

ListView recepeListView = getListView();
        recepeListView.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) 
            {
                CookingDataModel cook = recpeList.get(position);

                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putParcelable("Cooking",cook);
                intent.putExtras(bundle);
                intent.setClass(RecipeList.this,RecipeIngredientDetail.class);
                startActivity(intent);

            }
        });

Here is code of the CookingDataModel class.

public class CookingDataModel implements Parcelable{

    private RecipeDataModel recipe = null;
    private ArrayList<IngredientDataModel> ingredientList = null;

    public RecipeDataModel getRecipe() {
        return recipe;
    }
    public void setRecipe(RecipeDataModel recipe) {
        this.recipe = recipe;
    }
    public ArrayList<IngredientDataModel> getIngredientList() {
        return ingredientList;
    }
    public void setIngredientList(ArrayList<IngredientDataModel> ingredientList) {
        this.ingredientList = ingredientList;
    }

    public static final Parcelable.Creator<CookingDataModel> CREATOR = new Parcelable.Creator<CookingDataModel>() 
    {
        public CookingDataModel createFromParcel(Parcel in) 
        {
            return new CookingDataModel(in);
        }

        public CookingDataModel[] newArray(int size) 
        {
            return new CookingDataModel[size];
        }
    };

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void writeToParcel(Parcel arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    public CookingDataModel(Parcel in) {
        // TODO Auto-generated constructor stub

    }

    public CookingDataModel() 
    {
    }
}

Please help me in this respect that I could proceed my project. Thanks in adavance.

user2391890
  • 639
  • 1
  • 8
  • 15
  • Few things that you dont need : CookingDataModel cook = new CookingDataModel(); – Gaurav Arora Oct 02 '13 at 12:31
  • Possible duplicate of: [this](http://stackoverflow.com/questions/2736389/how-to-pass-object-from-one-activity-to-another-in-android), [this](http://stackoverflow.com/questions/2906925/android-how-do-i-pass-an-object-from-one-activity-to-another) – Paresh Mayani Oct 02 '13 at 12:55

2 Answers2

0

On the main activity:

intent.putExtra("Cooking",cook);

Then on second activity:

getIntent().getExtras().getParcelable("Cooking");

Try this. Do not use bundle if you've just to send a single object.

You may need to cast the getIntent().... part, I'm not sure.

Reinherd
  • 5,476
  • 7
  • 51
  • 88
0

Few things that need change.

  1. No need of creating a new object, it will be overwritten.

    CookingDataModel cook = new CookingDataModel();

  2. Typecast when you get the Parcelable object from intent extras,

    cook = bundle.getParcelable("Cooking");

  3. Make Sure when you sending the object, it has valid receipe member. If you notice, you are able to get the CookingDataModel from the intent, and also the Receipe from this object but not able to get data from ReceipeModel. From the code in the sending activity, I cant really say if the CookingDataModel.Receipe is a valid object.

Gaurav Arora
  • 1,805
  • 13
  • 13
  • Please check my CookingDataModel class where I implements this class Parcelable – user2391890 Oct 02 '13 at 12:47
  • I saw the model but, I dont see where you are creating the CookingDataModel object and adding to ListAdapter – Gaurav Arora Oct 02 '13 at 13:23
  • At sender activity I have checked values of the CookingDataModel object that is cook and there is no problem in CookingDataModel object at sender side – user2391890 Oct 02 '13 at 13:32
  • From the code posted here, I can't really comment on what is on the sender side. Because even on the receiver side, DataModel is received, its the content inside that Model, which is causing the NPE. Can you post the code where you are populating the CookingDataModel (at Sender side). – Gaurav Arora Oct 03 '13 at 12:48