1

I am trying to send an array of Strings which basically include a path of a file on the device. First the user Selects Picture 1 and then Select Picture 2. Once the user completes that the array is then loaded and passed to the next activity. When tried receiving the variable returns NullPointer.

MainActivity:

case SELECT_PICTURE1:
    if (resultCode == RESULT_OK) {
        // code here that gets the image path
        // String leftImagePath contains the path of selected Image
        // Used toast to display the image path and it is not null
        finished = true;
    }
    break;

case SELECT_PICTURE2:
    if (resultCode == RESULT_OK) {
        //similar to Select Picture 1
        // String rightImagePath contains the path of selected Image
        if (finished == true) {
            Bundle b = new Bundle();
            b.putStringArray("stringArray", new String[] {
                LeftImageString, RightImageString });
            Intent i = new Intent(MainActivity.this, modifiedImage.class);
            i.putExtras(b);
            // finished = false;
        }
    }
    break;

ModifiedImage class:

Intent intent = getIntent();
_imagesPath = intent.getStringArrayExtra("IMAGES_PATH");
Bundle b= this.getIntent().getExtras();
_userImagePath = b.getStringArray("stringArray");


if (_imagesPath == null) {
    for (int i = 0; i <= 1; i++) {
         //null pointer on the following line because _userImagePath contains nothing.
        _imagesPath[i] = _userImagePath[i];
        _originalBitmaps[i] = BitmapFactory.decodeFile(_imagesPath[i]);
    }
}

Does anyone know what is it that I have done wrong?

2
  • where you are passing IMAGES_PATH key from First Activity.also add null checks before getting Bundle and values from intent in second Activity Commented Sep 21, 2013 at 15:42
  • @User1204501 I think he is much clear in his comment Commented Sep 21, 2013 at 16:31

2 Answers 2

2

This code is clearly broken:

if (_imagesPath == null) {
    for (int i = 0; i <= 1; i++) {
        _imagesPath[i] = _userImagePath[i];
        _originalBitmaps[i] = BitmapFactory.decodeFile(_imagesPath[i]);
    }
}

If you get into the body of the if statement, you know that _imagesPath is null - and yet you dereference it in the loop, without ever assigning a new value to the variable. It seems to me that you want to just clone the array:

if (_imagesPath == null) {
    _imagesPath = _userImagePath.clone();
}

... or even just copy the reference:

if (_imagesPath == null) {
    _imagesPath = _userImagePath;
}

I suspect you want to then unconditionally execute this line of code for each appropriate value of i - why would you only want to do that if _imagesPath had previously been null?

_originalBitmaps[i] = BitmapFactory.decodeFile(_imagesPath[i]);

(It's not clear to me where you're initializing _originalBitmaps either, but that's another matter.)

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you sir! Will try it first thing in the morning.
The reason why I am checking for the path if it is null is because the user has two choices. Capture images from camera and in that case the image path is loaded automatically OR select images from location. Is there another way about this?
@User1204501 YOu must check for null then for values
@User1204501: I'm afraid I didn't follow your comment clearly enough (in terms of what it meant for the code). Do you need to be able to differentiate between those two options in the second activity? Maybe you should just unconditionally put the data in the same named "slot" in the bundle.
@User1204501: I wouldn't say that - I'd say that the code you had before is clearly broken, but I've already suggested fixes for the problem you had. If you want answers about a different problem with the code, I suggest you ask a new question. (It's not a good idea to make one post ask multiple questions - particularly not if some of those only end up being asked in comments...)
|
1

you are not passing any IMAGES_PATH in Extra then why are you expecting the value in target activity

  Intent intent = getIntent();
    _imagesPath = intent.getStringArrayExtra("IMAGES_PATH"); 
    Bundle b= this.getIntent().getExtras();
    _userImagePath = b.getStringArray("stringArray");

if _imagesPath is null then what every you do with the _imagesPath like you are following in code will threw NPE

Just adding a not '!' changes everything

if (_imagesPath != null) {

    for (int i = 0; i <= 1; i++) {
         //null pointer on the following line because _userImagePath contains nothing.
        _imagesPath[i] = _userImagePath[i];
        _originalBitmaps[i] = BitmapFactory.decodeFile(_imagesPath[i]);
    }
}

1 Comment

the IMAGE_PATH is already passed from the MainActivity. I just did not include the full code as that was not causing the error.

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.