Reputation: 61
I am trying to add Strings dynamically into an ArrayList along with index position and then pass on the Arraylist as an extra in Intent.The process runs fine for first time and on next call to this activity would end up in Index out of bounds Exception. Please see the code below and suggest if this is a flaw in the code.
private ArrayList<String> imagesUriArray=new ArrayList<>();
static int imageIndex=-1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Some standard code goes here to capture the image and store it...
image = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), image);
selectedImage = Uri.fromFile(photo);
final String UriString = new String(selectedImage.toString());
imageIndex++;
195: imagesUriArray.add(imageIndex,UriString);
Intent email = new Intent(ScanActivity.this, EmailActivity.class);
email.putExtra("picture", imageInByte);
email.putStringArrayListExtra("Uri", imagesUriArray);
startActivity(email);
Logcat says:
20320-20320/com.example.kittu.ClientLync E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.kittu.ClientLync/com.example.kittu.ClientLync.ScanActivity}: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
at android.app.ActivityThread.access$2000(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3689)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
at java.util.ArrayList.add(ArrayList.java:152)
at com.example.kittu.ClientLync.ScanActivity.onActivityResult(ScanActivity.java:195)
at android.app.Activity.dispatchActivityResult(Activity.java:3908)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578
at android.app.ActivityThread.access$2000(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
Upvotes: 1
Views: 195
Reputation: 2417
I think problem is you are using
static int imageIndex=-1;
So first time it works because it increments and becomes 0, while size of ArrayList is 0;
On next call, it increments again and index becomes 1, while List is again of size 0.
So try removing static modifier
Upvotes: 0
Reputation: 86381
According to your stack trace, you're seeing an IndexOutOfBoundsException
, not an NPE.
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
You're seeing this because you've tried to insert in the list outside the allowed range. The allowed range is zero to the length of the list, inclusive. In this case, you've tried to add an element at index 1, but the list has zero elements.
From the documentation for ArrayList.add(int index, E element):
public void add(int index, E element)
...
Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
Upvotes: 0
Reputation: 393781
private ArrayList<String> imagesUriArray=new ArrayList<>();
static int imageIndex=-1;
..
imageIndex++;
imagesUriArray.add(imageIndex,UriString);
The first time onActivityResult
is called, imageIndex
is -1, so after incrementing it, you are adding an item to the first index (0) of the list.
However, since imageIndex
is static, the next time onActivityResult
is called for a different object, imageIndex
is 0 even though the list is empty, so you try to add an element to index 1 of an empty List, which throws the index out of bounds exception. If you change imageIndex
to be non-static, the problem will be resolved.
Another option to avoid the exception you are getting is to change imagesUriArray.add(imageIndex,UriString);
to imagesUriArray.add(UriString);
. This will add the element to the next available index of the list.
Upvotes: 1
Reputation: 13189
It is because you are trying to access to an index that it's not valid. Look in your log console it gives to you an
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
It's because you are trying to access to index 1, that doesn't exists.
Upvotes: 0
Reputation: 116
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#add(int,%20E) read the documentation. Explicitly is written that the add(int, E) method throws an IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
Upvotes: 0
Reputation: 5618
Try this , you are accessing the wrong index while inserting
private ArrayList<String> imagesUriArray=new ArrayList<>();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
imagesUriArray.add(imageIndex,UriString);
Intent email = new Intent(ScanActivity.this, EmailActivity.class);
email.putExtra("picture", imageInByte);
email.putStringArrayListExtra("Uri", imagesUriArray);
startActivity(email);
}
Upvotes: 0
Reputation: 1792
You are accessing an invalid index in the arraylist, for example, if your arraylist is empty and you try to insert in the index = 1 it will throw you an IndexOutOfBoundsException
Upvotes: 1