0
 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

7
  • How have you declared 'checkBoxValue' ? Commented Aug 12, 2010 at 5:15
  • the snippet should work without any exception assuming that the array is initialized with a value before being used. Commented Aug 12, 2010 at 5:21
  • @Moron: What is your edit good for? Commented Aug 12, 2010 at 6:30
  • 1
    @Chicco: It is good for proving that stackoverflow has race conditions or maybe I am too slow! Commented Aug 12, 2010 at 6:49
  • 1
    @chiccodoro: For a second, I thought you were quite a jerk until I saw his name... hehe. Commented Aug 12, 2010 at 7:00

6 Answers 6

2

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 :-)

Sign up to request clarification or add additional context in comments.

Comments

1

Are you sure the Exception is raised here ?

1 Comment

There is nothing wrong with the included code. His exception must be occuring elsewhere.
1

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

5 Comments

+1 it could throw an exception, when you are using different threads
I don't understand how multi-threading could possibly cause this exception. In the code shown, we have a reference to an array. It is impossible for any other thread to modify the length of that array. If the data type was a List, then I could see how the size of the list could change in a situation where another thread removes an item in the list concurrently. But the length of an array cannot be changed. Please explain how the multi-threading could raise the exception.
@Alderath - we don't know where checkBoxValue is declared. The array can't be modified but another thread could assign a new (smaller?) array to the variable.
@Andreas_D You are correct. If the checkBoxValue array is an instance variable, multi-threading can cause the exception. For some reason I assumed that the checkBoxValue array was a local variable (gotta love how the editor in Eclipse makes instance variables blue to prevent misconceptions like this). Hence, my previous comment should be discarded.
@Alderath: Good point, me too, I only thought of a local variable which would mean that a re-assignment would not affect this code.
0

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

1 Comment

Even 'improper' initialization would lead to this kind of error. The OP says, that two values from the array are printed, so the array is constructed and contains (at least) two elements.
0

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.

Comments

0

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

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.