• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang. Array Index OutOf Bounds Exception

 
Lijoy John
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
while(rs.next()){
data = new NameData(rs.getString(1),rs.getInt
(2));// this is name & nameid from NAME table.
v.addElement(data);
}
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.
Thanks,
Regards
John
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lijoy,
Try this:

Cheers
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic