0

I wrote some code to load a lot of pictures step by step into imageviews. But if i run the code i get Index Out of Bound Exception: Invalid index 16, size is 15

Here is my Code

int steps = 5;
int tmp = 0;
for (int j = 0; j < imageUrl.size(); j++) {
    if (imageUrl.size() < steps) {
        for (int i = 0; i < imageUrl.size(); i++) {
            buildPicture(imageUrl.get(tmp));
            imageUrl.remove(tmp);
            tmp++;
        }
    } else if (imageUrl.size() >= steps) {
        for (int i = 0; i < steps; i++) {
            buildPicture(imageUrl.get(tmp));
            imageUrl.remove(tmp);
            tmp++;
        }
    }
}

Please, can you help me fixed my code?

Binary Nerd
  • 13,872
  • 4
  • 42
  • 44
dudi
  • 5,523
  • 4
  • 28
  • 57
  • 1
    Have you tried stepping through your code with a debugger? – JonK Sep 26 '16 at 13:14
  • 2
    It's not clear from this code what you expect the value of `tmp` to be - what are you trying to achieve with it? Have you considered what it will be at the start of the second iteration of the outer loop? – Jon Skeet Sep 26 '16 at 13:16
  • yes, as exaple, i tried with a imageUrl size of 24, the loop terminated after 12 runs. – dudi Sep 26 '16 at 13:20
  • I have an ArrayList of X items. What I want is too take 5 items and put them into the imageView. The loop should repeat until the ende of ArrayList. I think tmp is my index for the ArrayList. – dudi Sep 26 '16 at 13:26
  • Not sure what you are trying to do but notice `t` is set to 0 only before the loop. Is that what you meant to do ? – c0der Sep 26 '16 at 14:03

4 Answers4

0

When size equals 15 the last element index is 14 because the index of first element is always 0.

krasinski
  • 69
  • 2
  • and how can i fix it? and why the loop terminate after midway of the ArrayList. I tried with ArryList size of 24 and 19. The loop terminates after 12 and 10 – dudi Sep 26 '16 at 13:36
0

Try this, the for loop runs once less, because the array starts at 0 and not 1: EDIT: I'm not really sure what you're trying to do, I don't understand why are you using temp instead of the current [i] value of the loops. Try this though, if not, I'm not sure...

   int steps = 5;
for (int j = 0; j < (imageUrl.size() - 1); j++) {
    if (imageUrl.size() < steps) {
        for (int i = 0; i < (imageUrl.size() - 1); i++) {
            buildPicture(imageUrl.get(i));
            imageUrl.remove(i);
        }
    } else if (imageUrl.size() >= steps) {
        for (int i = 0; i < steps; i++) {
            buildPicture(imageUrl.get(i));
            imageUrl.remove(i);
            i++;
        }
    }

}

SuperHanz98
  • 2,090
  • 2
  • 16
  • 33
  • Thanks for the hint. I tried it out but if i run the code, I get this: java.lang.IndexOutOfBoundsException: Invalid index 9, size is 9 – dudi Sep 26 '16 at 13:47
  • Does it say the index of 'something' or size of 'something'? – SuperHanz98 Sep 26 '16 at 13:48
  • No, sorry. This is the Log: Process: com.example.dudi.myidea, PID: 25617 java.lang.IndexOutOfBoundsException: Invalid index 11, size is 11 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at – dudi Sep 26 '16 at 13:55
0

Say imageUrl has a size of 24 like in your comment above. You're getting an out of bounds exception because tmp eventually becomes a value that's bigger than 24, and when you try to invoke imageUrl.get(tmp), you're attempting to get an element that doesn't exist and hence the exception is thrown.

I'm not entirely sure what you're trying to do here, but I hope this helps.

Brunaldo
  • 3
  • 1
  • 4
0

A short demonstration about reason behind the request to post an MCVE or SSCCE. An MCVE like:

public static void main(String[] args){

    //use a variable instead of imageUrl.size()
    int imageUrlsize = 15;
    int steps = 5;
    int tmp = 0;
    for (int j = 0; j < imageUrlsize; j++) {
        if (imageUrlsize < steps) {
            for (int i = 0; i < imageUrlsize; i++) {
                System.out.println("imageUrlsize < steps " + tmp);
                tmp++;
            }
        } else if (imageUrlsize >= steps) {
            for (int i = 0; i < steps; i++) {
                System.out.println("imageUrlsize >= steps "+ tmp);
                tmp++;
            }
        }
    }
}

Makes it very clear that for imageUrl of size 15 tmp grows to 74.

Community
  • 1
  • 1
c0der
  • 18,467
  • 6
  • 33
  • 65