简体   繁体   中英

I can't fix this: java.lang.ArrayIndexOutOfBoundsException

When I try to run the following code I get a ' java.lang.ArrayIndexOutOfBoundsException '

ResultSet res = null;
    String[] waarden = new String[20];
    int length = 0;
    String query = "SELECT rd_datum, rd_locatie FROM racedag;";
    res = Database.executeSelectQuery(query);
    try{
        while (res.next()){
            int i = 1;
            waarden[i] = res.getString(1);
            i++;
            waarden[i] = res.getString(2);
            i++;
        }

        }
    catch (Exception e){
            }
    finally{
        length = waarden.length;
    }
    String eindWaarden[] = new String[length];
    for (int i = 0; i<= length; i++){
        eindWaarden[i] = waarden[i];
    }
    return eindWaarden;
}

and I have no clue why.

You haven't shown where the exception is occurring, but for one thing you're only ever writing to waarden[1] and waarden[2] (as i is declared inside the while loop). I doubt that that's deliberate.

Also, your exception handling is terrible - just catching exceptions and doing nothing is a bad idea, as is catching Exception itself in most cases.

Then there's this problem:

String eindWaarden[] = new String[length];
for (int i = 0; i<= length; i++) {
    eindWaarden[i] = waarden[i];
}

That will always fail, because you're accessing eindWaarden[length] , which will be off the end of the array. Valid indexes are 0 to length - 1 inclusive. If you want to keep that loop (which I doubt) change the <= to < .

Oh, and length is always going to be 20, however many results you retrieved - because that's the length of waarden .

Also, while this declaration is valid :

String eindWaarden[]

it's generally discouraged as a matter of style. Keep all the type information together, just as you did for waarden .

Your code would be much better if you'd just use a List<String> instead:

String query = "SELECT rd_datum, rd_locatie FROM racedag;";
ResultSet res = Database.executeSelectQuery(query);
List<String> results = new ArrayList<String>();
// Removed try block: declare that your method throws SqlException or whatever
while (res.next()) {
    results.add(res.getString(1));
    results.add(res.getString(2));
}
// Change the method declaration to have a return type of List<String>
return results;

change this

for (int i = 0; i<= length; i++){
        eindWaarden[i] = waarden[i];
    }

to

for (int i = 0; i< length; i++){
        eindWaarden[i] = waarden[i];
    }

ArrayIndexOutOfBoundsException means that you are trying to access an element in an array with an invalid index (the index is outside of the range 0 to array.length - 1 ).

The bug is in this line:

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

That should have been:

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

在最后一个for循环中,您说i <= length,我认为应该是i <length

There are other problems with the code, the this is what's causing the exception:

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

Change the <= to < :

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

Replace

for (int i = 0; i<= length; i++){
    eindWaarden[i] = waarden[i];
}

With

for (int i = 0; i < length; i++){
    eindWaarden[i] = waarden[i];
}

( < instead of <= )

Edit :

I'm just gonna come out and say it. This code returns a 20-element array of String , of which the first element is empty, and the next two elements will be populated with the last row returned from the given query. All the other elements in the array are empty.

Since SQL doesn't necessarily guarantee the order of results (unless you specify an ORDER BY clause) you could get a random row each time.

This whole thing could be rewritten as:

final int length = 20;
String[] waarden = new String[20];
final String query = "SELECT rd_datum, rd_locatie FROM racedag LIMIT 1";
ResultSet res = Database.executeSelectQuery(query);

try {
    if (res.next()) {
        waarden[1] = res.getString(1);
        waarden[2] = res.getString(2);
    }
} catch (Exception e) {
    // handle exception
}

return waarden;

This probably isn't what you meant to do though...

Use < instead of <= in your for loop. Otherwise, you are trying to access an index which doesn't exist:

for (int i = 0; i< length; i++){
    eindWaarden[i] = waarden[i];
}
for (int i = 0; i<= length; i++){
    eindWaarden[i] = waarden[i];
}

replace with

for (int i = 0; i< length; i++){
    eindWaarden[i] = waarden[i];
}

This is the bug:

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

An array has N elements, with index going from 0 to N-1.

So the correct one is:

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

Besides the ArrayIndexOutOfBoundsException which is already addressed in the other answers, and the very bad error handling as mentioned by @JonSkeet there is also this piece of code

try{
    while (res.next()){
        int i = 1;
        waarden[i] = res.getString(1);
        i++;
        waarden[i] = res.getString(2);
        i++;
    }

    }
catch (Exception e){
        }
finally{
    length = waarden.length;
}

Is there any good reason your waarden array has a length of 20 if you only put values at index 1 and index 2 . Looking at the rest of your code you expect waarden to get fully populated with the results of your query, which will not be the case. You probably want to move the int i = 1 outside the while loop and initialize it at 0 instead of 1 . This will only work if you are certain the number of results will never exceed 10 or your waarden[] is simply too small

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM