1

It is regarding to this post. I trying to display 9 icon, 9 textField, but I get error

java.lang.ArrayIndexOutOfBoundsException: 9

Below are the tab code

static void addIt(JTabbedPane tabbedPane, String text) throws IOException {

        JPanel panel = new JPanel(new GridBagLayout());
        gbc = new GridBagConstraints();

        foodLabel = new JLabel[ELEMENTS];
        qtyField = new JTextField[ELEMENTS];
        file = new File[ELEMENTS];
        imageIcon = new ImageIcon[ELEMENTS];
        image = new BufferedImage[ELEMENTS];

        for (int i = 0; i < ELEMENTS; i++) {
            try {
                file[i] = new File("C:\\Users\\tony\\Desktop\\MedSalad.png");
                file[i + 1] = new File("C:\\Users\\tony\\Desktop\\JapanesePanNoodles.png");
                file[i + 2] = new File("C:\\Users\\tony\\Desktop\\Spaghetti.png");
                file[i + 3] = new File("C:\\Users\\tony\\Desktop\\PadThai.png");
                file[i + 4] = new File("C:\\Users\\tony\\Desktop\\RamenNoodles.png");
                file[i + 5] = new File("C:\\Users\\tony\\Desktop\\SpaghettiAndMeatBalls.png");
                file[i + 6] = new File("C:\\Users\\tony\\Desktop\\chickenRice.jpg");
                file[i + 7] = new File("C:\\Users\\tony\\Desktop\\thaiFood.jpeg");
                file[i + 8] = new File("C:\\Users\\tony\\Desktop\\vietnamFood.jpg");

                image[i] = ImageIO.read(file[i]);
                imageIcon[i] = new ImageIcon(image[i]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

         for (int i = 0; i < ELEMENTS; i++) {
            foodLabel[i] = new JLabel(imageIcon[i]);
            qtyField[i] = new JTextField(3);
         }
            gbc.gridx =0;
            for (int i = 0; i < ELEMENTS; i++) {
                if (i % 3 == 0) {
                    gbc.gridy += 2;
                    gbc.gridx = 0;
                }
                panel.add(foodLabel[i], gbc);
                gbc.gridy++;
                panel.add(qtyField[i], gbc);
                gbc.gridx++;
                gbc.gridy--;
                tabbedPane.addTab(text, panel);
    }
} 

Error pointed to

file[i + 1] = new File("C:\\Users\\tony\\Desktop\\JapanesePanNoodles.png");
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
John Joe
  • 12,412
  • 16
  • 70
  • 135

4 Answers4

2

You define file as:

    file = new File[ELEMENTS];

But then access it here like this:

    for (int i = 0; i < ELEMENTS; i++) {
        try {
            file[i] = new File("C:\\Users\\tony\\Desktop\\MedSalad.png");
            file[i + 1] = new File("C:\\Users\\tony\\Desktop\\JapanesePanNoodles.png");
            file[i + 2] = new File("C:\\Users\\tony\\Desktop\\Spaghetti.png");
            file[i + 3] = new File("C:\\Users\\tony\\Desktop\\PadThai.png");
            file[i + 4] = new File("C:\\Users\\tony\\Desktop\\RamenNoodles.png");
            file[i + 5] = new File("C:\\Users\\tony\\Desktop\\SpaghettiAndMeatBalls.png");
            file[i + 6] = new File("C:\\Users\\tony\\Desktop\\chickenRice.jpg");
            file[i + 7] = new File("C:\\Users\\tony\\Desktop\\thaiFood.jpeg");
            file[i + 8] = new File("C:\\Users\\tony\\Desktop\\vietnamFood.jpg");

            image[i] = ImageIO.read(file[i]);
            imageIcon[i] = new ImageIcon(image[i]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

When i is ELEMENTS - 1, file[i + 1] will be file[ELEMENTS], which will be out of bounds, because the last element in the array has index ELEMENTS - 1

What you probably wanted to do is this:

    file = new File[ELEMENTS];
    ...
    file[0] = new File("C:\\Users\\tony\\Desktop\\MedSalad.png");
    file[1] = new File("C:\\Users\\tony\\Desktop\\JapanesePanNoodles.png");
    file[2] = new File("C:\\Users\\tony\\Desktop\\Spaghetti.png");
    file[3] = new File("C:\\Users\\tony\\Desktop\\PadThai.png");
    file[4] = new File("C:\\Users\\tony\\Desktop\\RamenNoodles.png");
    file[5] = new File("C:\\Users\\tony\\Desktop\\SpaghettiAndMeatBalls.png");
    file[6] = new File("C:\\Users\\tony\\Desktop\\chickenRice.jpg");
    file[7] = new File("C:\\Users\\tony\\Desktop\\thaiFood.jpeg");
    file[8] = new File("C:\\Users\\tony\\Desktop\\vietnamFood.jpg");

    for (int i = 0; i < ELEMENTS; i++) {
        try {
            image[i] = ImageIO.read(file[i]);
            imageIcon[i] = new ImageIcon(image[i]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Sentry
  • 4,102
  • 2
  • 30
  • 38
2

It seems as though files should just be initialized hard-codedly, not in a loop:

file[0] = new File("C:\\Users\\tony\\Desktop\\MedSalad.png");
file[1] = new File("C:\\Users\\tony\\Desktop\\JapanesePanNoodles.png");
file[2] = new File("C:\\Users\\tony\\Desktop\\Spaghetti.png");
file[3] = new File("C:\\Users\\tony\\Desktop\\PadThai.png");
file[4] = new File("C:\\Users\\tony\\Desktop\\RamenNoodles.png");
file[5] = new File("C:\\Users\\tony\\Desktop\\SpaghettiAndMeatBalls.png");
file[6] = new File("C:\\Users\\tony\\Desktop\\chickenRice.jpg");
file[7] = new File("C:\\Users\\tony\\Desktop\\thaiFood.jpeg");
file[8] = new File("C:\\Users\\tony\\Desktop\\vietnamFood.jpg");

for (int i = 0; i < ELEMENTS; i++) {
    try {
        image[i] = ImageIO.read(file[i]);
        imageIcon[i] = new ImageIcon(image[i]);
    } catch (Exception e) {
        e.printStackTrace(); // Or some other way to handle the exception
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

Create an array of all your image file name, then use that array in your loop.

String items[]=new String[]{"MedSalad.png","JapanesePanNoodles.png","Spaghetti.png","PadThai.png","RamenNoodles.png","SpaghettiAndMeatBalls.png","chickenRice.jpg","thaiFood.jpeg","vietnamFood.jpg"};

for (int i = 0; i < ELEMENTS; i++) {
    try {
        file[i] = new File("C:\\Users\\tony\\Desktop\\"+items[i]);
        image[i] = ImageIO.read(file[i]);
        imageIcon[i] = new ImageIcon(image[i]);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
1

The reason is you have declared an array of size n (the size of elements) and you are trying to access the index n + 8. ---> file[i + 8] <---

assuming size of elements = 8, therefore index position = 8-1 = 7, file[i + 8] = file[7 + 8] = file[15].

therefore the size of your array file should have been 16 (15 +1) if size of elements = 8.

but i think that you should review the for loop if you really need it..

Christopher
  • 425
  • 3
  • 9