Android Studio - Value must be ≥ 0
Asked Answered
C

3

38

I am getting an error in Android Studio to do with my Cursor.

I have the following line in my code

String data = cursor.getString(cursor.getColumnIndex(columnIndex));

columnIndex is being passed into the method.

This part cursor.getColumnIndex(columnIndex) produces the following error

Value must be ≥ 0

Its happening in my DBHelper class and also my recycler adapter when it uses a cursor too.

It shows up as an error in red but the app still builds and runs without issue.

Any ideas?

Thanks for any help.

Update 22-Sep-21

I'm adding some code as requested and also how i have got around this error. Not sure if its the best way though.

So the method im using is this....

public String getTripInfo(String tableName, int tripNo, String columnIndex){
    String data = "";

    // Select all query
    String selectQuery = "SELECT * FROM " + tableName + " WHERE " + TRIP_DETAILS_TRIP_NUMBER + "=" + tripNo;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // Looping through all rows and adding to list
    if(cursor.moveToFirst()){
        do{
            data = cursor.getString(cursor.getColumnIndex(columnIndex));
        } while(cursor.moveToNext());
    }

    // Closing connections
    cursor.close();
    db.close();

    //Returning number plates
    return data;
}

The error is in the do while loop. The part in red is "cursor.getColumnIndex(columnIndex))"

The way i have gotten around this error is using the following code instead

public String getTripInfo(String tableName, int tripNo, String columnIndex){
    String data = "";

    // Select all query
    String selectQuery = "SELECT * FROM " + tableName + " WHERE " + TRIP_DETAILS_TRIP_NUMBER + "=" + tripNo;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // Looping through all rows and adding to list
    if(cursor.moveToFirst()){
        do{
            int index = cursor.getColumnIndex(columnIndex);
            data = cursor.getString(index);
        } while(cursor.moveToNext());
    }

    // Closing connections
    cursor.close();
    db.close();

    //Returning number plates
    return data;
}
Court answered 4/9, 2021 at 6:55 Comment(7)
That means columnIndex is sometimes negative, check out the method passing columnIndex. Table column index must not be negative or greater than last column index.Paradies
Thanks for your response. So according to the documentation, -1 can be returned as shown below. So I'm still not sure what i can do to fix this error though. Any chance you could explain a little further? getColumnIndex(String columnName) Returns the zero-based index for the given column name, or -1 if the column doesn't exist.Court
cursor.getColumnIndex takes String parameter which is table name you get its column index. So, you can pass proper parameter as you see. Can you share whole code related to that issue?Paradies
Please provide enough code so others can better understand or reproduce the problem.Kurtkurth
Just use : cursor.getColumnIndexOrThrow(...). The original issue is a lint problem - running ./gradlew lint will show this. This must be happening with the latest AS release - Arctic Fox 2020.3.1 Patch 2 - With zero code changes after updating to this patched version I can no longer build a project for this reason - if you look at the changelog it mentions a fix around lint checks : androidstudio.googleblog.com/2021/09/… & issuetracker.google.com/issues/197146610?pli=1 could be this ...Sterilize
@MarkKeen I have this exact same issue, and I think you are correct in that it arises after Patch 2. I haven't touched anything in the Java class, and it is now throwing this error in the lint check.Raina
@Kurtkurth Ive added some extra code. Also this wasn't an issue with a previous version of Android Studio. I cant comment here on which version it was working on but currently im on 2020.3.1 Patch 2Court
A
37

The problem is that cursor.getColumnIndex() can return -1, you're passing it as a direct parameter, and the cursor getters need a column index gte 0. The getters' parameter is annotated with @IntRange(from = 0) which is why lint marks it as an error.

So, even though you might have built your project such that it would never produce an invalid column index, the fact that such a possibly could exist is why lint is tagging it.

Your code revision only avoids the issue. It would be best to use cursor.getColumnIndexOrThrow() or test index for gte 0.

You can get away with your changes since you know more about your project than lint does, it just isn't the best practice.

Alvey answered 15/11, 2021 at 18:37 Comment(0)
U
49

I had an error like this.
My solution : change method getColumnIndex into getColumnIndexOrThrow.

Uniaxial answered 2/11, 2021 at 13:12 Comment(1)
is it not from the comment of @SterilizeDisavow
A
37

The problem is that cursor.getColumnIndex() can return -1, you're passing it as a direct parameter, and the cursor getters need a column index gte 0. The getters' parameter is annotated with @IntRange(from = 0) which is why lint marks it as an error.

So, even though you might have built your project such that it would never produce an invalid column index, the fact that such a possibly could exist is why lint is tagging it.

Your code revision only avoids the issue. It would be best to use cursor.getColumnIndexOrThrow() or test index for gte 0.

You can get away with your changes since you know more about your project than lint does, it just isn't the best practice.

Alvey answered 15/11, 2021 at 18:37 Comment(0)
H
0

Use This Method Proper Work :-

@SuppressLint("Range") Strong name = cursor.getString(cursor.getColumnIndex(indexNumber));

Husk answered 31/3, 2022 at 10:37 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Kurtkurth

© 2022 - 2024 — McMap. All rights reserved.