aspose file tools*
The moose likes Beginning Java and the fly likes IndexOutOfBoundsException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "IndexOutOfBoundsException" Watch "IndexOutOfBoundsException" New topic
Author

IndexOutOfBoundsException

Wang Yang
Greenhorn

Joined: Oct 11, 2002
Posts: 5
When I access a ArrayList by get()
I got the following exception
"IndexOutOfBoundsException index 80 size 100"
I refer to the source code of java.util.ArrayList

But from the exception info,we know the index<size
What's wrong with it?
Thanks in advance!
[ May 09, 2004: Message edited by: Wang Yang ]
Stefan Krompass
Ranch Hand

Joined: Apr 29, 2004
Posts: 75
Hi,
can you post a minimal sample of your code where the exception occurs?
cu
Stefan
[ May 09, 2004: Message edited by: Stefan Krompass ]
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154

If index = 80 and size = 100 then the condition will not evaluate to true, so the exception will not occur. So... perhaps index is not really 80 (e.g. maybe it's -80) or size is not really 100 (e.g. maybe it's 10) or your JVM has been hit by a stray cosmic ray.
Or are you using it in a multi-threaded application? The methods in ArrayList are not synchronized, so it is possible (though you'd have to be reasonably unfortunate) that another thread updates the list at the same time as you're getting its contents. If this is the case, either synchronize or use a Vector instead.
Wang Yang
Greenhorn

Joined: Oct 11, 2002
Posts: 5
Originally posted by David Peterson:

If index = 80 and size = 100 then the condition will not evaluate to true, so the exception will not occur. So... perhaps index is not really 80 (e.g. maybe it's -80) or size is not really 100 (e.g. maybe it's 10) or your JVM has been hit by a stray cosmic ray.
Or are you using it in a multi-threaded application? The methods in ArrayList are not synchronized, so it is possible (though you'd have to be reasonably unfortunate) that another thread updates the list at the same time as you're getting its contents. If this is the case, either synchronize or use a Vector instead.

Thank you every much!
It is the trap of Multi-thread.
I'll pay more attention to it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: IndexOutOfBoundsException