adjieq
adjieq

Reputation: 99

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 101

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

Answers (4)

user2515832
user2515832

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

dat3450
dat3450

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

Maxim Tulupov
Maxim Tulupov

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

Tim Biegeleisen
Tim Biegeleisen

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

Related Questions