0

I'm getting an array index out of bound exception on the following code. I used debugger and this is what happened.


JLabel[][] labelHolder = new JLabel[8][8]; 

    for(int i=0; i<8; i++){
        for(int j=0; i<8; j++){
            labelHolder[i][j] = new JLabel ();   <- error occur right on this line when i=j=0

I have not clue why this is because if I just swap out the i and j for 0 and 0, it work perfectly :S

1
  • Four duplicate answers... Commented Apr 10, 2013 at 17:12

4 Answers 4

4

The condition in your second for-loop checks against the value of i instead of j

for(int j = 0; i < 8 ; j++)

should be

for(int j = 0; j < 8 ; j++)
Sign up to request clarification or add additional context in comments.

Comments

3

Use:

for(int j=0; j<8; j++){
             ^

Comments

1

for(int j=0; i<8; j++){ This line has the problem, it should read j<8.

Comments

1

In your second loop, your stop condition is mistaken: replace i < 8 by j < 8.

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.