Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

GetIntent is giving me a null pointer and crushing my app ribbit

i want to know if there is another way i can write this code to prevent it from giving me this error.

mMediaUri = getIntent().getData();
    mFileType = getIntent().getExtras().getString(ParseConstants.KEY_FILE_TYPE);
    mMyMessage = getIntent().getExtras().getString("themessage");

3 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Can you paste in all the messages in your logcat? Sounds like something is missing.

04-18 00:05:29.851 7194-7194/com.instantmedia.swerve W/dalvikvm? threadid=1: thread exiting with uncaught exception (group=0x40d2b2a0) 04-18 00:05:29.867 7194-7194/com.instantmedia.swerve E/AndroidRuntime? FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.instantmedia.swerve/com.instantmedia.swerve.RecipientsActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2129) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154) at android.app.ActivityThread.access$700(ActivityThread.java:146) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4949) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.instantmedia.swerve.RecipientsActivity.onCreate(RecipientsActivity.java:54) at android.app.Activity.performCreate(Activity.java:5185) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)             at android.app.ActivityThread.access$700(ActivityThread.java:146)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)             at android.os.Handler.dispatchMessage(Handler.java:99)             at android.os.Looper.loop(Looper.java:137)             at android.app.ActivityThread.main(ActivityThread.java:4949)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:511)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)             at dalvik.system.NativeStart.main(Native Method) Ben Jakuben

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Okay, if you filter through all those messages you can find the important one:

java.lang.NullPointerException at com.instantmedia.swerve.RecipientsActivity.onCreate(RecipientsActivity.java:54)

So next, paste in your code for RecipientsActivity. Which line is at line 54? Mark it with a comment since we won't have your same line numbers in here.

this is my code at line 54

mMediaUri = getIntent().getData();
    //mFileType = getIntent().getExtras().getString(ParseConstants.KEY_FILE_TYPE);
    mMyMessage = getIntent().getExtras().getString("themessage");

Ben Jakuben

Ben Jakuben
Ben Jakuben
Treehouse Teacher

I'm guessing that the Extra with the key "themessage" is missing or has a slightly different key. First, compare this to where you are putting the "themessage" extra on the Intent. You want to make sure it's the same key going in and coming out here.

Then you could add a line and add a break point and run the debugger. If you add an Intent variable, like this:

mMediaUri = getIntent().getData();
Intent intent = getIntent();
mMyMessage = getIntent().getExtras().getString("themessage");

then you could debug at the "Intent intent" line and take a look at the intent variable itself. You could verify if it has an extra named "themessage" or not.