Search code examples
androidnullpointerexceptionandroid-bundle

Android passing parameters using Bundle is returning null


I pass a parameter to my new activity in android:

Intent intent = new Intent(this,InOccasion.class);
intent.putExtra("key",key); //I've checked an seen that in this activity "key" isn't null
this.startActivity(intent);

As I said, key in this block isn't null.

However, when I retrieve it:

Bundle b = getIntent().getExtras();
if (b!=null)
  key = b.getString("b");

key is null and is causing a NullPointerException. However, I'm sure key isn't null. And yes, I am calling from the correct activity.

I've seen this but it's unanswered and mine is ALWAYS null.


Solution

  • You are setting they key name "key" and getting "b". Get the right key i.e. "key"