File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes ArrayIndexOutOfBound Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayIndexOutOfBound" Watch "ArrayIndexOutOfBound" New topic


khurram babar

Joined: May 13, 2003
Posts: 4
any body tell me when this occurs plz tell me with brief description
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

I believe this occurs when you try and access an array element that does not exist. For example, say you had:
String[] i = {"Gregg", "Henry", "Jo"};
and then you said:
String name = i[3];
You would probably get that error because element 3 is "out of the array bounds".

GenRocket - Experts at Building Test Data
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

an ArrayIndexOutOfBoundsException happens when .... ummmm.. you're accessing an array and you go out of bounds (helpful huh? )
But no, seriously -- that's exactly what happens.
Lets say you have a snazzy li'l int array that holds 5 ints like this:
int intArray[] = {0, 1, 2, 3, 4};
(now remember -- arrays start at index 0)
So... if I try to access spaces 0 thru 4, everything's fine -- no problems -- there are numbers in all those spots, and all those spots actually EXIST.
System.out.println( intArray[0] );
System.out.println( intArray[1] );
System.out.println( intArray[2] );
System.out.println( intArray[3] );
System.out.println( intArray[4] );
But what happens if I try to access BEYOND that last spot?
System.out.println( intArray[5] );
*boom* ArrayIndexOutOfBoundsException
get it?
So... what do you think will happen if you try to access a negative index? like this:
System.out.println( intArray[-1] );

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
jim gotti
Ranch Hand

Joined: Jul 02, 2002
Posts: 36
the ole +1 -1 error. remember array indecies(spelling?) run from 0 to n-1, where n is the length of the array at creation.
So as the above posters pointed out....
an array of length say 5, has indecies from 0 to 4 (n-1)
I agree. Here's the link:
subject: ArrayIndexOutOfBound
It's not a secret anymore!