7

I have some error reports on my Android app, it's a Nullpointerexception in onCreate() in an Activity. The code that fails is getIntent().getExtras().getStringExtra("name"). (Nullpointerexception)

That means that getExtras() is null somehow. I am sure that I am setting the intent extra on every place I am creating the intent. I can't recreate it on my emulator on device. I think it happened on my real device (but not while I was debugging) after I tried to open the app again, in the mean time Android probably killed the process and recreated the activity again. But shouldn't be the intent extras kept even in this scenario?

I tried to kill the process on the emulator, onCreate was called again and the getExtras() returned the right value.

I replaced the code with getIntent().getStringExtra(). What's the difference besides it won't throw a nullpointerexception but will still set the String as null. Is there any other difference?

What could be causing it?

Marco Ferrari
  • 4,914
  • 5
  • 33
  • 53
Daniel Novak
  • 2,746
  • 3
  • 28
  • 37

3 Answers3

8

Intent.getStringExtra() returns null if there are no extras. Intent.getExtras() returns null if there are no extras, so you need to check for that before trying to call getString() or other methods on it.

hackbod
  • 90,665
  • 16
  • 140
  • 154
  • Yes, but how can it happen that getExtras is sometimes (very rarely) null? I am setting extras in my code everytime. – Daniel Novak May 01 '11 at 23:58
  • Are you sure? Do you see "has extras" in the logcat? Do you know for sure how your particular activity was invoked? – EboMike May 02 '11 at 07:25
  • If it returns null, there are no extras, for whatever reason. We can't see your code, but the most likely answer is somewhere your code is allowing there to be no extras. – hackbod May 02 '11 at 17:44
  • Ok, I will mark this as answered. But I still think there is something wrong, not in the code itself - it's just an Activity, openned from another activity and always with extras. I remember that it once failed, I switched to another application after a long time I returned to my activity and it resulted in an error. onCreate was called again and no extras were found (on the same activity that had extras before I switched to another application!) – Daniel Novak May 02 '11 at 21:59
0

getIntent.getExtras() is returning null at some point. getIntent().getStringExtra() is most likely coded to check for a null extras and to return null if getExtras() is null. So called "no throws" architecture. If so, this is valid runtime execution and would not throw an exception. calling getStringExtra on a null extras is not valid runtime execution and should throw an exception.

It appears that there IS a path where getExtras() is null, so you could code around this by checking for a null extras.

JAL
  • 3,319
  • 2
  • 20
  • 17
0

I found out that somewhere else in my code I was creating shortcut intents which had a String[] extra. Android HOME (and maybe other parts of the system) don't persit string arrays, only primitive extras (int, string, long, float...). But the code that was causing problems doesn't use shortcuts, it's only a simple activity which receives a string array extra. Maybe the empty extras were caused by this problem too - the application was killed by OS and the extras could not be stored. No exception is thrown by Android in this case.

Described by Romain Guy here: http://groups.google.com/group/android-developers/browse_thread/thread/7f2ce458bd5d112f/189e2b7b2b2532d7

I asked a more specific question about this here: Shortcut intent extras lost after restart?

Community
  • 1
  • 1
Daniel Novak
  • 2,746
  • 3
  • 28
  • 37