简体   繁体   中英

java.lang.ArrayIndexOutOfBoundsException: 9 error

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");

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();
        }
    }

It seems as though file s 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
    }
}

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();
    }
}

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM