65

Is there any way to check if an extra has been passed when starting an Activity?

I would like to do something like (on the onCreate() in the Activity):

    Bundle extras = getIntent().getExtras();
    String extraStr = extras.getString("extra");

    if (extraStr == null) {
        extraStr = "extra not set";
    }

But this is throwing a java.lang.NullPointerException.

Thank you.

jalv1039
  • 1,593
  • 2
  • 16
  • 21
  • You can use if(extras.getString("extra") == null) {extraStr = "extra not set";}. In your code NullPointerException occurs at String extraStr = extras.getString("extra"). – Pankaj Kumar Nov 24 '11 at 10:42
  • 1
    Not really. `NullPointerException` is throwing at `extras.getString("extra")` not when assigning it to `extraStr`. So the solution is what Michal Kottman said. – jalv1039 Nov 24 '11 at 11:04
  • Have you read my comment carefully? What code I have written in my if condition? It clearly saying that the same as you are teaching me. And in second part of my comment, I was indicating that you exception occurs at that line. – Pankaj Kumar Nov 24 '11 at 11:22
  • I think I haven't explained very well. What I wanted to say is that the `NullPointerException` error is throwing inside the `extras.getString("extra")` call. This call doesn't return me a `null` value, it just throws the error before returning any result. So the check makes no sense because it crashes before. I hope it is clear now. – jalv1039 Nov 30 '11 at 00:55

5 Answers5

199

Use the Intent.hasExtra(String name) to check if an extra with name was passed in the intent.

Example:

Intent intent = getIntent();

if (intent.hasExtra("bookUrl")) {
    bookUrl = b.getString("bookUrl");
} else {
   // Do something else
}

Also, use Intent.getStringExtra(String name) directly on the intent to handle the NullPointerException if no extras were passed.

Michal Kottman
  • 15,379
  • 2
  • 41
  • 61
13

Well, I had similiar problem. in my case the null point exception was happen when I checked if my bundle.getString() was equall to null.

here is how IN MY CASE I solved it:

Intent intent = getIntent();        
    if(intent.hasExtra("nomeUsuario")){
        bd = getIntent().getExtras();
        if(!bd.getString("nomeUsuario").equals(null)){
            nomeUsuario = bd.getString("nomeUsuario");
        }
    }
JúlioCézar
  • 427
  • 5
  • 8
  • `!bd.getString("nomeUsuario").equals(null)` seems like a bug. It'd throw an NPE if the value was e.g. a Number. – fncomp Mar 02 '18 at 16:04
7
if (this.getIntent().getExtras() != null && this.getIntent().getExtras().containsKey("yourKey")) {
   // intent is not null and your key is not null
}
sagits
  • 5,030
  • 36
  • 42
6

I think you need to check when extras != null

Bundle extras = getIntent().getExtras();
   if (extras != null) {
        String extraStr = extras.getString("extra");
    }else {
        extraStr = "extra not set";
    }
Inzimam Tariq IT
  • 5,936
  • 8
  • 32
  • 61
tvtruong
  • 101
  • 1
  • 4
5

I would use this solution in your case.

String extraStr;
    try {
        extraStr = getIntent().getExtras().getString("extra");
    } catch (NullPointerException e ) {
        extraStr = "something_else";
    }
Exide
  • 51
  • 1
  • 5