Kalpana.V
Kalpana.V

Reputation: 11

Java Array concept

 for(int m=0; m< checkBoxValue.length ; m++)
 {
    System.out.println("check box in model class"+checkBoxValue[m]+"\n");

 }

This loop is to print two values in array. It prints the values But after that it shows array out of bound exception

Upvotes: 0

Views: 763

Answers (6)

Andreas Dolk
Andreas Dolk

Reputation: 114847

Maybe you have overridden the toString() method of the checkBoxValue-class (the array initializer would help identifying this class). Following this theory, the toString() implementation might work fine for the first two elements of the array (they are printed) and may throw an exception for the third element in the array.

This could lead to the error description: This loop is to print two values in array. It prints the values But after that it shows array out of bound exception

Upvotes: 0

Alderath
Alderath

Reputation: 3874

The posted code should not throw ArrayIndexOutOfBoundsException. Most likely, you are doing something after the loop which accesses an incorrect index of an array.

The only way that the code shown in the question could throw an ArrayIndexOutOfBoundsException is if the toString() method of one of the checkBoxValue[m] objects throws the exception.

Upvotes: 0

rick
rick

Reputation: 19

Ohh.. Looks like a mess. The information looks very abstract. You need to be specific, may be you can give more code over here. One possible cause I think of, may be, is Multi-threading.

Only multi-threaded application can do this trick. If so, you better provide synchronization on the origin object of checkBoxValue variable.

Hope that helps....

Upvotes: 1

Kamal
Kamal

Reputation: 4088

The code should work fine provided you have done the array initialization correctly.

Upvotes: 0

chiccodoro
chiccodoro

Reputation: 14716

It seems you're on the wrong track. It's best to set a breaking point on your for loop and debug your code, then go through it step wise. This will reveal where the exception is thrown...

Especially since you say "after that", you might want to review your code after that for loop :-)

Upvotes: 2

thelost
thelost

Reputation: 6704

Are you sure the Exception is raised here ?

Upvotes: 1

Related Questions