-2

I get an ArrayIndexOutOfBoundsException, but only when the getSubImage() function is there.

This is to loop through and get sprites.I've tried it without getsubimage and i've tried using different for loops though nothing has helped.

This is not the same as the thread I am being compared to, as I am using a for loop inside of another. The program works just fine without getSubimage, and the for loops work just fine. I do not know where the problem lies or what it is.

        try {

        BufferedImage spritesheet = ImageIO.read(
            getClass().getResourceAsStream(
                "/Sprites/SpriteSheet.gif"          
                    )
        );

        sprites = new ArrayList<BufferedImage[]>();
        for(int i = 0; i < 6; i++) {

            BufferedImage[] bi =
                new BufferedImage[i];

            for(int j = 0; j < numFrames[i]; j++) {

                    bi[j] =    spritesheet.getSubimage(
                            i * width,
                            j* height,
                            width,
                            height
                    );

                }
                sprites.add(bi);

            }


        }


    catch(Exception e) {
        e.printStackTrace();
    }

I expect it to loop through this and get all the sprites. What actually happens is it doesnt get the subimage and it just throws an out of bounds exception.

Danny C
  • 1
  • 4

1 Answers1

0

Your program is trying to retrieve an element with index not available from an array. Check the line 15 (is numFrames already created some where?). It possible that you are not using a right array or numFrames does not have an element with that specific index. Try running a debugger and see which array is out of index.