Reputation: 99
I am trying to see the contents of an array, but when I run it comes an error message
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 101
Does anyone know where the problem is? here is my code:
public final class KonversiQtoH extends DebitDiberikan{
public KonversiQtoH() throws SQLException{
QBeriPintu1();
}
Connection c = KoneksiDatabase.getKoneksi();
Float[] fine;
public void QBeriPintu1() throws SQLException{
try{
List rowValues = new ArrayList();
Statement s = c.createStatement();
String sql = "SELECT LA40 from tabel_debit";
ResultSet rs = s.executeQuery(sql);
while (rs.next()){
rowValues.add(rs.getFloat("LA40"));
}
rs.close();
s.close();
fine = (Float[]) rowValues.toArray(new Float[rowValues.size()]);
}catch(SQLException e){
System.err.println("QKanan, error");
}
}
public static void main(String[] args) throws SQLException{
KonversiQtoH j = new KonversiQtoH();
j.QBeriPintu1();
for (int i=0; i<=j.fine.length; i++){
int a = i+1;
System.out.println(a+". "+j.fine[i]);
}
}
}
Upvotes: 0
Views: 361
Reputation:
You should remove the "=" i<=j.fine.length
for (int i=0; i<j.fine.length; i++){
int a = i+1;
System.out.println(a+". "+j.fine[i]);
}
Upvotes: 1
Reputation: 954
The following line produces errors:
for (int i=0; i<=j.fine.length; i++) {
Replace with:
for (int i=0; i<j.fine.length; i++) {
Upvotes: 2
Reputation: 1786
Change here
for (int i=0; i<=j.fine.length; i++){
int a = i+1;
System.out.println(a+". "+j.fine[i]);
}
to
for (int i=0; i<j.fine.length; i++){
int a = i+1;
System.out.println(a+". "+j.fine[i]);
}
Comparison operatore from <= to <
Upvotes: 1
Reputation: 521997
This appears to be the problematical code:
for (int i=0; i <= j.fine.length; i++) {
int a = i+1;
System.out.println(a+". "+j.fine[i]);
}
Arrays in Java are indexed from zero to the length minus one. The above loop actually tries to access one beyond the length of the array. Fixing the bounds should get rid of this error:
for (int i=0; i < j.fine.length; i++){
int a = i+1;
System.out.println(a+". "+j.fine[i]);
}
Upvotes: 1