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?
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.)
See more on this question at Stackoverflow