Well, this is my first post on here... I've found your message boards very useful when searching for problems so I decided to register and ask for help on this one since it's a little more specific than what I could find...

I've had 3 semesters of programming in college and I'm learning gooie programming on my own..
Here's the code where I keep getting stuck (btw, this is part of a bigger program -- im actually making a minesweeper game, but this is what you need to know from the area I have my problem). I made it so you can just copy and paste it to test on your own:

public class test extends JFrame{
	GameButton[][] grid = new GameButton[16][30];
	public static void main(String[] args){
		test m = new test();

	private class GameButton extends JButton{
		private GameButton(int num){
			this.num = num;
		private int num;
	public void foo(){
		for (int i = 0; i<grid.length; i++){
			for (int j = 0; j<grid[0].length; i++){
				grid[i][j] = new GameButton(i);
				grid[i][j].addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae){


the arrayindexoutofboundsexception comes up when adding action listeners to grid[j] (line 20)... I'm just curious because I cant find why this happens.... I think it has something to do with the 2d array.. If I use a 1d array like in another game I wrote, it works... It also doesnt matter if I set the for loops for i and j to reach 2 or the length... Anyone think they can help or at least explain?

8 Years
Discussion Span
Last Post by jasimp

Look at the following two lines

for (int i = 0; i<grid.length; i++){
    for (int j = 0; j<grid[0].length; i++){

Which variable is being incremented at the end of the second for loop? Is that the one you wanted to increment? I don't think so.

Edit: And I'm just too slow! ;-)


wow... stupid mistake :)

I was so boggled why this wasn't working... thanks.... :)


Don't worry about it. Happens all the time. Let me guess, a cut-n-paste error. ;-)

Or the fact that on most keyboards I and J are right next to each other, diagonally. Also when in lowercase they look similar.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.