简体   繁体   中英

Why am I getting java.lang.ArrayIndexOutOfBoundsException

So basically, I'm trying to make a grid of 144 buttons (12x12) by making 12 rows down with 12 buttons along each row.

However, when I try to run, I get this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at progrid.me.myname.grid.ProGrid.main(ProGrid.java:23)

Here's the code:

package progrid.me.myname.grid;

import javax.swing.*;

public class ProGrid extends JFrame {   
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static void main(String[] arguments) {
        JFrame frame = new JFrame(); //Create the frame
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("My ProGrid remake");
        frame.setSize(300, 300);

        JPanel[] row = new JPanel[12];
    for(int i = 0; i < 12; i++){
        frame.add(row[i]);
            row[i].setBounds(0, i*25, 300, 25);
            row[i].setVisible(true);


            for(int j = 0; i < 12; i++){
                JButton[] button = new JButton[j]; //Make 12 new JButtons (button[0] - button[12]) for each row
                row[i].add(button[j]);
                button[j].setBounds(i*25, 0, 25, 25);
                button[j].setVisible(true);


            }
        }

    }

}

Line 23 is this:

frame.add(row[i]);

Which probably means this won't work either:

row[i].add(button[j]);

So could anyone give me a little shove in the right direction? I'm not asking for you to dump code at me, I'm actually trying to learn. But some help would be great. Thanks!

EDIT Fixed the first problem, now I'm getting java.lang.NullPointerException on line frame.add(row[i]);

problem:

JPanel[] row = new JPanel[i];

You are creating a new array of JPanel and JButton in each iteration of your loop, which results in an ArrayIndexOutOfBoundsException on the first iteration of your loop for i=0 when it executes JPanel[] row = new JPanel[0]; frames.add(row[0]); JPanel[] row = new JPanel[0]; frames.add(row[0]);

solution:

For both arrays, create only one instance of the array outside the loop, and update that instance in each iteration of the loop. Here's an example for the JPanel array:

JPanel[] row = new JPanel[12]; //Make 12 new JPanels (row[0] - row[12])
for(int i = 0; i < row.length; i++){
        frame.add(row[i]);
        row[i].setBounds(0, i*25, 300, 25);
        row[i].setVisible(true);

EDIT:

JPanel[] row = new JPanel[12];
    for (int i = 0; i < 12; i++) {
        row[i] = new JPanel();
        frame.add(row[i]);
        row[i].setBounds(0, i * 25, 300, 25);
        row[i].setVisible(true);

        JButton[] button = new JButton[12];
        for (int j = 0; i < 12; i++) {
            button[j] = new JButton();
            row[i].add(button[j]);
            button[j].setBounds(i * 25, 0, 25, 25);
            button[j].setVisible(true);

        }
    }

Row[0] to row[12] will create 13 rows. Make it row[11]

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