Volupion
Volupion

Reputation: 21

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.

Upvotes: 0

Views: 4741

Answers (11)

Jonathan
Jonathan

Reputation: 7604

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

Upvotes: 0

Robin
Robin

Reputation: 36611

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

Upvotes: 0

f2lollpll
f2lollpll

Reputation: 1007

In the last for loop you say i <= length, which I believe should just be i < length

Upvotes: 1

Jesper
Jesper

Reputation: 206816

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++){

Upvotes: 1

Savino Sguera
Savino Sguera

Reputation: 3572

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++){

Upvotes: 0

Balaswamy Vaddeman
Balaswamy Vaddeman

Reputation: 8530

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

replace with

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

Upvotes: 0

dogbane
dogbane

Reputation: 274612

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

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500675

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;

Upvotes: 7

Guillaume
Guillaume

Reputation: 22822

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 <=)

Upvotes: 0

Zohaib
Zohaib

Reputation: 7116

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

Upvotes: 1

NPE
NPE

Reputation: 500367

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++){

Upvotes: 0

Related Questions