User1204501
User1204501

Reputation: 831

NullPointer when sending strings between different Packages

I am trying to pass a string array of _imagesPaths from one package to another.

I have tried the following:

//sending the paths of images from `MainActivity` which is in `main.packages`
//assume the array is not null

Intent b = new Intent(MainActivity.this, EditPicturesActivity.class);
b.putExtra("left",LeftImageString);
b.putExtra("right",RightImageString);

The paths are received in another package by doing the following: private String[] _imagesPath; Bundle extras = getIntent().getExtras();
_imagesPath[0] = extras.getString("left"); _imagesPath[1] = extras.getString("right");

Next, I try to load images supplied by the paths but I get a NullPointer which says _imagesPath is null.

EDIT

The value of _imagesPath is assigned by doing selecting an image from gallery: In this activity

private String[] _imagesPath = null;

case SELECT_PICTURE1:
        if (resultCode == RESULT_OK) {

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            LeftImageString = cursor.getString(columnIndex);

            cursor.close();

            //the toast displays the path and it is not null
            Toast.makeText(
                    getApplicationContext(),
                    "The path of the first image you have selected is:  "
                            + LeftImageString, Toast.LENGTH_SHORT).show();  
            // String leftImagePath contains the path of selected Image


            //intent for "left" is placed here

        }      
        break; 

//similary image is taken for Image 2.

Upvotes: 0

Views: 76

Answers (3)

Yogendra
Yogendra

Reputation: 5260

Your code is fine, but you need to initialize your image array.

Thank you.

Upvotes: 0

Sivakumar S
Sivakumar S

Reputation: 681

you can directly pass _imagesPaths like,

Intent b = new Intent(MainActivity.this, EditPicturesActivity.class);
b.putExtra("paths",_imagesPaths);

And can get it on other end like,

String[] paths = getIntent().getStringArrayExtra("paths");

Upvotes: 0

hasan
hasan

Reputation: 24205

you must initialise _imagesPath before you set values to it:

String _imagesPath[] = new String[2];
Bundle extras = getIntent().getExtras();            
_imagesPath[0] = extras.getString("left");
_imagesPath[1] = extras.getString("right");

Upvotes: 0

Related Questions