ArrayList Error on Index

This is my code;

/*

* Main2.java

*

* Created on 09 February 2007, 14:40

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package testing;

/**

*

* @author Administrator

*/

import java.sql.*;

import java.util.ArrayList;

import java.util.ListIterator;

/**

*

* @author Administrator

*/

publicclass Main2{

Connection conn=null;

Statement stmt=null;

ResultSet rsBankName=null;

int valid,invalid,total;

/** Creates a new instance of Main */

public Main2(){

connectOracle();

readCards(2007,1,31);

//System.out.println(checkDate(""));

}

/**

* @param args the command line arguments

*/

publicvoid connectOracle(){

try{

Class.forName("oracle.jdbc.driver.OracleDriver");

conn = DriverManager.getConnection("jdbc:oracle:thin:@IP:SID","user","pwd");

stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

}catch (Exception e){

e.printStackTrace();

}finally{}

}

publicvoid readCards(int year,int month,int day){

String bankCode="";

ArrayList<String> bname=new ArrayList<String>(10);

try{

rsBankName=stmt.executeQuery("select bank_code,bank_name from bank");

rsBankName.first();

do{

bname.add(rsBankName.getInt("bank_code"),rsBankName.getString("bank_name"));

}while(rsBankName.next());

}catch (SQLException ex){

ex.printStackTrace();

}

ListIterator li=bname.listIterator();

while(li.hasNext()){

System.out.println(li.next());

}

}

publicstaticvoid main(String[] args){

new Main2();

}

}

And I am getting this Stack trace

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

at java.util.ArrayList.add(ArrayList.java:367)

at testing.Main2.readCards(Main2.java:60)

at testing.Main2.<init>(Main2.java:36)

at testing.Main2.main(Main2.java:71)

I search for it on the net, it say it has a bug and it was version 1.2 and I am using the new version of JDK 1.6.

Any idea what is going wrong.

[4403 byte] By [Khadsa] at [2007-11-26 17:55:05]
# 1

I'm not sure what your exact problem is, but most likely you are not returning any records. The preferred method of handling ResultSets is not your way, but like this.

try {

rsBankName=stmt.executeQuery("select bank_code,bank_name from bank");

while(rsBankName.next())

{

bname.add(rsBankName.getInt(1),rsBankName.getString(2));

};

~Tim

SomeoneElsea at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...
# 2

There is records in the database, and if i debug it i can see the record retrieved.

I search on google on this, they say it is bug in version 1.2, Well i hope it fixed by 1.6, but I think it still excist as a bug.

Still getting the same error even if i do this

int i=1;

do{

bname.add(i++,"sss");

}while(rsBankName.next());

Khadsa at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...
# 3
Try to use vector or ListList a=new ArrayList()
G_Abubakra at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...
# 4

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

at java.util.ArrayList.add(ArrayList.java:367)

at testing.Main2.readCards(Main2.java:57)

at testing.Main2.<init>(Main2.java:34)

at testing.Main2.main(Main2.java:68)

same same same errror! Any other idea please, Any other alternatives

Khadsa at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...
# 5

change

do {

}while()

loop to while () {

}

as said in a previous reply.....

1) Do while executes the loop at least one time......... (even if there are no results)

2) While checks whether bool condition is true and then only executes loop..

If no records are there, it wont execute...

Its so simple

saracgia at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...
# 6

The size is zero (you have set the capacity, not the size in the ctor), you attempt to add something at location 1.

I can not see any "set size" type method in the API. Fill it with nulls first?

> I search for it on the net, it say it has a bug

> and it was version 1.2 and I am using the

> new version of JDK 1.6.

Can you post a link to the bug report? Ta.

Message was edited by:

mlk

mlka at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...
# 7
saracgi that is not the case with my loop, Im sure about it, well put it 85%>I can not see any "set size" type method in the API. Fill it with nulls first?mlk I didnt get you!There is no function setSize(), and how can I fill it with null? can you give me a hint
Khadsa at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...
# 8

Yes i got it, I just put this and now it work,

...

String bankCode="";

List<String> bname=new ArrayList<String>();

bname.add(null);

...

Thank you all for you support

Khadsa at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...
# 9

> The size is zero (you have set the capacity, not the

> size in the ctor), you attempt to add something at

> location 1.

>

> I can not see any "set size" type method in the API.

> Fill it with nulls first?

>

> > I search for it on the net, it say it has a bug

> > and it was version 1.2 and I am using the

> > new version of JDK 1.6.

>

> Can you post a link to the bug report? Ta.

>

> Message was edited by:

> mlk

D'OH!!

You can't add an element at a specific index unless there is already something at that index ? WTF? This is strange behavior, even though I will say that there are other classes you could use instead. Try a HashMap<Integer,String>

~Tim

SomeoneElsea at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...
# 10

> D'OH!!

>

> You can't add an element at a specific index unless

> there is already something at that index ? WTF? This

> is strange behavior,

What is even odder is that you can expand by one with "add", just not greater than that.

ArrayList<Integer> thingies = new ArrayList<Integer>(10);

for(int i = 0; i< 10; i++ ) {

thingies.add( i, i );

}

System.out.println( thingies );

works, but

ArrayList<Integer> thingies = new ArrayList<Integer>(10);

for(int i = 9; i>0; i-- ) {

thingies.add( i, i );

}

System.out.println( thingies );

does not.

> even though I will say that

> there are other classes you could use instead. Try a

> HashMap<Integer,String>

Or if the size is fixed, use an array.

mlka at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...
# 11
Right, my earlier comment was not quite right, since the OP was trying to add an element at index 1 . If he had been adding it to index 0, everything would have been OK. Learn something new every day!~Tim
SomeoneElsea at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...
# 12
My intension was something different...i had something like this+| bank code | Bank name|| 11111|abc|| 2222 | fasdf|i want to extract bank code into index, and bank name into object,well i get it didnt work..
Khadsa at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...
# 13
Put it more open, i wanted to put something like thisbname.add(rsBankName.getInt("bank_code"),rsBankName.getString("bank_name"));because i want to decrease my search time... =)
G_Abubakra at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...
# 14
Hashmap. (Or leave it in the DB?)
mlka at 2007-7-9 5:08:12 > top of Java-index,Java Essentials,New To Java...