【问题标题】:java.lang.ArrayIndexOutOfBoundsException: 9 errorjava.lang.ArrayIndexOutOfBoundsException: 9 错误
【发布时间】:2017-06-24 14:33:21
【问题描述】:

关于这个post。 我试图显示 9 个图标,9 个文本字段,但出现错误

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

【问题讨论】:

  • 哪一行抛出异常? ELEMENTS的价值是什么?
  • ELEMENTS 为 9。错误指向file[i] = new File("C:\\Users\\tony\\Desktop\\MedSalad.png");
  • 你确定错误没有指向下面的一行吗?
  • @Sentry 感谢您的指出。帖子已更新。

标签: java arrays user-interface indexoutofboundsexception


【解决方案1】:

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

【讨论】:

  • 我也是这么想的,但是 OP 说错误在 file[i] 这没有意义,虽然代码需要逻辑改革
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      原因是您声明了一个大小为 n(元素大小)的数组,并且您正试图访问索引 n + 8。---> file[i + 8]

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

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

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

      【讨论】:

        猜你喜欢
        • 2022-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多