Android Studio - Value must be ≥ 0

Viewed 11983

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;
}
3 Answers

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

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.

Use This Method Proper Work :-

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

Related