Android getIntent().getExtras() returns null

I'm trying to pass a string between two activities. I've done this in other projects using the same method, but for some reason I'm getting a NullPointerException when I call intent.getStringExtra(String). I have also tried creating a Bundle for the extras via

Bundle b = getIntent().getExtras();

but that also returned null. Below is the code that I am currently trying to use.

Activity A:

Intent myIntent = null; 
    String select = "";
            if (selection.equals("Chandelle")) {
                myIntent = new Intent(Commercial.this, Chandelle.class);
                select = "Chandelle";
            } else if (selection.equals("Eights on Pylons")) {
                myIntent = new Intent(Commercial.this, EightsOnPylons.class);
                select = "Eights on Pylons";
            }
 // Start the activity
    if (myIntent != null) {
        myIntent.putExtra("selection", select);
        Log.d("*** OUTBOUND INTENT: ", "" + myIntent.getExtras().get("selection"));
        startActivity(myIntent);
    }

Here's the code in activity B that tries to pull the extra:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    Intent i = getIntent();

    if (i == null) 
        Log.d("***DEBUG****", "Intent was null");
    else
        Log.d("**** DEBUG ***", "Intent OK");

    String MANEUVER_ID  = i.getStringExtra("selection"); //Exception points to this line
    Log.d("*** DEBUG", rec + " " + MANEUVER_ID);

I've tried pretty much every alternative way of passing extras, but they all seem to behave this way. What am I missing?


Add .ToString() to myIntent.putExtra("selection", select); so that it is myIntent.putExtra("selection", select.ToString());


Luckily I found this post. I've been struggling with this for hours and in the end I realized that I'm sending my intent to the tabHost as well. ;-) For those who are still having problems with this just get the extras from the tabHost activity itself and pass it along to the child tabs.

String userID;

. .

 Bundle getuserID = getIntent().getExtras();
    if (getuserID != null) {
        userID = getuserID.getString("userID");
    }

...

 intent = new Intent().setClass(this, Games.class);
    intent.putExtra("userID", userID);
    spec = tabHost.newTabSpec("games").setIndicator("Games",
                      res.getDrawable(R.drawable.tab_games))
                  .setContent(intent);
    tabHost.addTab(spec);

Try the code below. I have added an override to your code, which will do the trick:

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState); 

   Intent i = getIntent();

   if (i == null) 
       Log.d("***DEBUG****", "Intent was null");
   else
       Log.d("**** DEBUG ***", "Intent OK");

   String MANEUVER_ID  = i.getStringExtra("selection"); //Exception points to this line
   Log.d("*** DEBUG", rec + " " + MANEUVER_ID);
}

Look at your code, I think it may happen when you did put anything in Activity 1. Because you use "if...else if", and you don't have "else" for other cases. If that happens then you will get null value in the Activity 2. Check again this case.