This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

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;
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

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

Cheers
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
 
wood burning stoves
 
subject: java.lang. Array Index OutOf Bounds Exception
 
Similar Threads
java.lang.Array IndexOut OfBounds Exception
Merging rows with same ID together with dynamic headers with CSV
jtable
How to store resultset field value into array?
Please help me in my program