wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Array IndexOutOfBounds Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Array IndexOutOfBounds Exception" Watch "Array IndexOutOfBounds Exception" New topic
Author

Array IndexOutOfBounds Exception

unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
how do i fix this please?
java.lang.ArrayIndexOutOfBoundsException: 50
at IntFile1.readInteger(IntFile1.java:54)




Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
I noticed that when you assign the array element, you're not checking the array bounds. I recommend checking that x is less than the length of your array before assigning a value to an array element with index x. That should fix your problem.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
In other words, instead of


I would use:



There are better ways to do this, but this solution at least checks the array bounds to make sure you don't exceed the array length.
[ November 10, 2004: Message edited by: Jeff Bosch ]
unyime inok
Greenhorn

Joined: Oct 13, 2004
Posts: 29
Thanks a lot. i'll try doing that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array IndexOutOfBounds Exception