When I start my CalendarPopUp Activity, I get a NullPointerException.
Here’s my code.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setUpWindow();
Intent intent = getIntent();
Bundle extras = intent.getExtras(); // This is Line 37
boolean isForResult = extras.containsKey("result");
//...
}
Here’s the exception
07-30 09:33:47.269: E/AndroidRuntime(19979): FATAL EXCEPTION: main
07-30 09:33:47.269: E/AndroidRuntime(19979): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.moveinblue.planner/com.moveinblue.planner.utils.popup.CalendarPopup}: java.lang.NullPointerException
07-30 09:33:47.269: E/AndroidRuntime(19979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
07-30 09:33:47.269: E/AndroidRuntime(19979): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
07-30 09:33:47.269: E/AndroidRuntime(19979): at android.app.ActivityThread.access$600(ActivityThread.java:132)
07-30 09:33:47.269: E/AndroidRuntime(19979): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
07-30 09:33:47.269: E/AndroidRuntime(19979): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 09:33:47.269: E/AndroidRuntime(19979): at android.os.Looper.loop(Looper.java:137)
07-30 09:33:47.269: E/AndroidRuntime(19979): at android.app.ActivityThread.main(ActivityThread.java:4575)
07-30 09:33:47.269: E/AndroidRuntime(19979): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 09:33:47.269: E/AndroidRuntime(19979): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 09:33:47.269: E/AndroidRuntime(19979): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-30 09:33:47.269: E/AndroidRuntime(19979): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-30 09:33:47.269: E/AndroidRuntime(19979): at dalvik.system.NativeStart.main(Native Method)
07-30 09:33:47.269: E/AndroidRuntime(19979): Caused by: java.lang.NullPointerException
07-30 09:33:47.269: E/AndroidRuntime(19979): at com.moveinblue.planner.utils.popup.CalendarPopup.onCreate(CalendarPopup.java:37)
07-30 09:33:47.269: E/AndroidRuntime(19979): at android.app.Activity.performCreate(Activity.java:4465)
07-30 09:33:47.269: E/AndroidRuntime(19979): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-30 09:33:47.269: E/AndroidRuntime(19979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
07-30 09:33:47.269: E/AndroidRuntime(19979): ... 11 more
Can anybody help me here?
After debugging I know intent’s mExtras object is null (intent is actually not null, but most of its inner objects are). Of course when calling getExtras() it returns null, and when calling containsKey() it will throw a NullPointerException.
The
getExtras()method returns null, if no extra has been added to the intent (documentation). Try to add thatextrato the intent first, then start theCalendarPopUpActivity, then extract thisBundlefrom theIntent.EDIT:
If you don’t always need that extra, check it first like this
this way you will be able to detect if there is an extra in the intent, and you are saved from the
NullPointerException