java.lang.ArrayIndexOutOfBoundsException:9错误

时间:2017-06-24 14:33:22

标签: java arrays user-interface indexoutofboundsexception

关于这个post。 我试图显示9个图标,9个textField,但是我收到了错误

  

java.lang.ArrayIndexOutOfBoundsException:9

以下是标签代码

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

错误指向

file[i + 1] = new File("C:\\Users\\tony\\Desktop\\JapanesePanNoodles.png");

4 个答案:

答案 0 :(得分:2)

您将file定义为:

    file = new File[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();
        }
    }

iELEMENTS - 1时,file[i + 1]将为file[ELEMENTS],这将超出范围,因为数组中的最后一个元素具有索引ELEMENTS - 1

您可能想要做的是:

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

答案 1 :(得分:2)

似乎file s应该只是硬编码而不是循环初始化:

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

答案 2 :(得分:2)

创建一个包含所有图像文件名的数组,然后在循环中使用该数组。

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

答案 3 :(得分:1)

原因是你声明了一个大小为n的数组(元素的大小),并且你试图访问索引n + 8. ---&gt; file [i + 8]&lt; ---

假设元素的大小= 8,因此索引位置= 8-1 = 7, file [i + 8] = file [7 + 8] = file [15]。

因此,如果元素的大小= 8,则数组文件的大小应为16(15 +1)。

但我认为你应该检查for循环,如果你真的需要它..