Saturday, July 23, 2011

[android-developers] Curious NPE

I am getting some bugreports of NPE's which I cannot explain.

These happen rarely. Here's the must baffling (in simplified code):

class MyActivity extends Activity
{
Object myObject;

protected void onCreate(Bundle state)
{
myObject = new Object();
}

public void onBackPressed()
{
myObject.myMethod();
}
}

Then the user gets a NPE on myObject when it tries to call myMethod in
obBackPressed()

There are no other myObject assignemnet in my code -- once it's
inialized, it's never touched. So the only way I can see that NPE
happenining is if onBackPressed() is called before onCreate().

I've had some other reports where the NPE happened after a screen
rotate, but it involved a member variable, which should have been also
initalized in onCreate().

Since these reports are rare with only 1 report per error, I suspect a
race conditiion.

Would love to hear people's ideas. (And please don't tell me to chech
myObject != null before accessing)

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment