Reputation: 1301
So I have this code that iterates through an ArrayList and adds its contents to an Imageview. I'm checking to see whether the ArrayList is empty at a certain index to stop it from processing the code, but the thing is that its crashing whenever the condition is checking, namely this picturePathResults.get(i) != null giving me an out of bounds exception. This is usually happening when the ArrayList has one element in it, it loops once through the first and only element and then crashes when checking during the second loop. I don't understand why its crashing when the condition is supposed to prevent this very scenario from happening.
for(int i=0; i < allEDs.size(); i++){
LinearLayout singleOptionContainer =new LinearLayout(this.getApplicationContext());
ETResults[i] = allEDs.get(i);
if(picturePathResults.size() != 0 ){
if(picturePathResults.get(i) != null){
picturePathResult = picturePathResults.get(i);
imagesContainer = new ImageView(QuestionCollector.this);
imagesContainer.setId(imageViewTemplateID);
imagesContainer.setBottom(editTextTemplateID);
imagesContainer.setLayoutParams(imageParams);
image = BitmapFactory.decodeFile(picturePathResult);
scaledBitmap = scaleImage(image, 330, 330, 330, true);
imagesContainer.setImageBitmap(scaledBitmap);
singleOptionContainer.addView(imagesContainer);
}else{
return;
}
}
}
Upvotes: 1
Views: 3977
Reputation: 1
You check correctly, if there is something in picturePathResults
, but that does not mean that an element with the index i
exists. Moreover, get(i)
does not change the size of picturePathResults
, you may want to use remove(i)
.
Upvotes: 0
Reputation: 496
Look at ArrayList documentation will tell you why you get exception: http://developer.android.com/reference/java/util/ArrayList.html#get(int)
You are trying to iterate the ArrayList of size you do not know in for loop with number of iterations which might be different from the size of picturePathResults so if your index will be >= picturePathResults.size() then BOOM you get exception.
if((picturePathResults.get(i) != null)&&(i < picturePathResults.size()))
This should help
Upvotes: 0
Reputation: 381
you check whether picturePathResults
has some elements in it - then get the i
th element.
replace if(picturePathResults.size() != 0 ){
with if(picturePathResults.size() > i ){
this should stop attempting to get an index that doesnt exist
Upvotes: 5
Reputation: 682
Collet89 already answered how to quickly correct your conditional statement such that you shouldn't run out of bounds, but it's worth clarifying that your check of picturePathResults.get(i) != null
won't be able to perform the check you want.
The problem is that you're trying to see if the array value at i
exists, but in order to do this, you must request the value at i
, so your code will throw an IndexOutOfBoundsException
before it even gets to the null check. As Collet89 implied, you need to check the metadata of your Array (its size) before attempting to access a value to ensure you won't go out of bounds.
Your check is verifying that the value contained in picturePathResults.get(i)
is not null, but it will fail if the array isn't long enough to have a value at index i
Upvotes: 3