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

	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){
					  //performClickAction((GameButton)ae.getSource());

					}
				});
				//add(grid[i][j]);
			}
		}
	}
}

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?

Recommended Answers

All 6 Replies

I don't think you want to increment "i" here

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

:P

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

Ha! You're too slow masijade!!
:)

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.