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");
4
  • Which line throws the exception? And what is the value of ELEMENTS? Commented Jun 24, 2017 at 14:34
  • ELEMENTS is 9. The error pointed to file[i] = new File("C:\\Users\\tony\\Desktop\\MedSalad.png"); Commented Jun 24, 2017 at 14:36
  • Are you sure the error doesn't point to one line below that? Commented Jun 24, 2017 at 14:39
  • 1
    @Sentry thanks for pointing out. Post updated. Commented Jun 24, 2017 at 14:40

4 Answers 4

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();
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

i thought the same too , but OP said error is at file[i] which makes no sense , though code need logic reformation
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
    }
}

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.