Java nested for () loop throws ArrayIndexOutOfBoundsException
I am working on a JPanel exercise in a Java book. I am tasked with creating a 5x4 grid using a GridLayout.
When I browse the container to add panels and buttons, the first add () throws an OOB exception. What am I doing wrong?
package mineField;
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class MineField extends JFrame {
private final int WIDTH = 250;
private final int HEIGHT = 120;
private final int MAX_ROWS = 5;
private final int MAX_COLUMNS = 4;
public MineField() {
super("Minefield");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container mineFieldGrid = getContentPane();
mineFieldGrid.setLayout(new GridLayout(MAX_ROWS, MAX_COLUMNS));
// loop through arrays, add panels, then add buttons to panels.
for (int i = 0; i < MAX_ROWS; i++) {
JPanel[] rows = new JPanel[i];
mineFieldGrid.add(rows[i], rows[i].getName());
rows[i].setBackground(Color.blue);
for (int j = 0; j < MAX_COLUMNS; j++) {
JButton[] buttons = new JButton[i];
rows[i].add(buttons[j], buttons[j].getName());
}
}
mineFieldGrid.setSize(WIDTH, HEIGHT);
mineFieldGrid.setVisible(true);
}
public int setRandomBomb(Container con)
{
int bombID;
bombID = (int) (Math.random() * con.getComponentCount());
return bombID;
}
/**
* @param args
*/
public static void main(String[] args) {
//int randomBomb;
//JButton bombLocation;
MineField minePanel = new MineField();
//minePanel[randomBomb] = minePanel.setRandomBomb(minePanel);
}
}
I'm pretty sure I reprogrammed a simple nested loop. As I am new to Java, please. I'm sure I'll come back someday.
a source to share
JPanel[] rows = new JPanel[i];
i
is 0 on the first iteration, which is not what you want. Do it:
JPanel[] rows = new JPanel[MAX_ROWS];
Also, I think you want to completely accept this outside of the loop for
, since you seem to be using its elements that would be uninitialized ...
This is also incorrect:
JButton[] buttons = new JButton[i];
i
maybe 0 when j
equal to 2, in which case there is no such thing as a buttons[j]
. Do them all MAX_*
and I think you want to take them out of the loop as I see no point in re-creating them on every iteration. Also, you need to instantiate the individual elements of the array.
a source to share
This part doesn't make sense:
for (int j = 0; j < MAX_COLUMNS; j++) {
JButton[] buttons = new JButton[i];
rows[i].add(buttons[j], buttons[j].getName());
}
You are creating a i
JButtons array and trying to add j
th to rows
, which makes little sense and won't work if j >= i
. You probably wanted:
JButton[] buttons = new JButton[MAX_COLUMNS];
for (int j = 0; j < MAX_COLUMNS; j++) {
rows[i].add(buttons[j], buttons[j].getName());
}
But the array still doesn't contain any buttons, all you did was initialize it. There is really no reason for an array; this actually works:
for (int j = 0; j < MAX_COLUMNS; j++) {
JButton button = new JButton("foo");
rows[i].add(button, button.getName());
}
a source to share
The problem is that your button array is of size i, but j may be larger than i. For example, on first run, you make an empty array here:
JButton[] buttons = new JButton[i];
because i equals 0. Then you try to access it at index 0, which doesn't exist (since the array has no size) and you get your exception. If you instead do something like:
JButton[] buttons = new JButton[MAX_COLUMNS];
This way you will have a button for each location of the array. Also, you probably need to initialize individual buttons - for example, something like this:
for (int k = 0; k < MAX_COLUMNS; k++) {
buttons[k] = new JButton();
}
(disclaimer: code not tested, but pulled-know-where from you for example only. There may be typos or invisible errors.)
Good luck.
a source to share
It looks like you are creating too many arrays. You create your INSIDE arrays for loops, so instead of creating 5 lines, you create 5 lines 5 times, or 25 lines.
Another problem is that you are not actually creating any objects, just an array to hold the objects. For each object in your array, you need a different line "button [j] = new JButton ()".
a source to share