Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes java.lang. Array Index OutOf Bounds Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.lang. Array Index OutOf Bounds Exception" Watch "java.lang. Array Index OutOf Bounds Exception" New topic

java.lang. Array Index OutOf Bounds Exception

Lijoy John
Ranch Hand

Joined: Jul 24, 2001
Posts: 32
Hi to All,
It would be kind if anyone could let me know , why i am getting this exception in the following code :
Vector v = new Vector();
NameData data = null;
data = new NameData(rs.getString(1),rs.getInt
(2));// this is name & nameid from NAME table.
int iSize = v.size();// here v.size is 1
String[][] names = new String[iSize][iSize];
for(int i=0; i < iSize; i++){
data = (NameData)v.get(x);
names[i][0] = data.getName();// this
is 'MyName'
(The below lines throws
Array Index OutOf Bounds Exception)
names[i][1] = Integer.toString(data.getNameId
()); // this is 11899
I am not really clear why this exception is coming up when the size is 1. When the size is more than 1 for example 10 then this exception is not thrown.
Any replies will be appreciated.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Lijoy,
Try this:

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Remember that array indices start at zero. If the size of the array is 1 then 0 is the only valid index. Any other value is out of bounds. In other words, when you use 1 as an index, the exception is thrown.

Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: java.lang. Array Index OutOf Bounds Exception
It's not a secret anymore!