0

I am currently encountering a NullPointerException. I have used log statements to realize that it it shuts down when one specific line of code is read.

I have read lots about this type of exception, however I don't understand what is wrong with this specific statement.

try {

    Log.i(TAG, "1"); // Breaks here
    mDiaryId = ContentUris.parseId(launchingIntent.getData());
    Log.i(TAG, "2");

} catch (NumberFormatException e) {
    Log.i(TAG, "3");
    mDiaryId = -1;
}   
1
  • Have you debug it yet? Probably it has something to do with launchingIntent being null Commented Feb 26, 2014 at 20:10

1 Answer 1

1

You need to check for null after calling launchingIntent.getData(). Like this.

try {

    Log.i(TAG, "1");
    Uri data = getIntent().getData();
    if (data != null) { // <-- check data for null
        mDiaryId = ContentUris.parseId(data);
    } else {
        mDiaryId = -1;
        Log.i(TAG, "Data is null");
    }
    Log.i(TAG, "2");

} catch (NumberFormatException e){
    Log.i(TAG, "3");
    mDiaryId = -1;
} 
Sign up to request clarification or add additional context in comments.

5 Comments

I feel the launchingIntent itself is null and hence trying to do launchingIntent.getData() is throwing a NPE.
If your launching intent is taken from getIntent(), then it's never null.
Ohh you modified.Didnt see that. :) I was trying pointing out use of launchingIntent directly to barry.
Yeah that's worked, however I now have the same error further down. I will get my head around it one day. Thank you
Just remember, that getData() returns null, if your activity gets called by an intent without data assigned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.