• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

IndexOutOfBoundsException

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
can you post a minimal sample of your code where the exception occurs?
cu
Stefan
[ May 09, 2004: Message edited by: Stefan Krompass ]
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic