Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio - Value must be ≥ 0

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;
}
like image 581
Justin Avatar asked Sep 04 '21 06:09

Justin


People also ask

What does cursor moveToFirst do?

Calling moveToFirst() does two things: it allows you to test whether the query returned an empty set (by testing the return value) and it moves the cursor to the first result (when the set is not empty).

What is the use of cursor in Android?

Introduction to Cursor in Android The basic purpose of a cursor is to point to a single row of the result fetched by the query. We load the row pointed by the cursor object. By using cursor we can save lot of ram and memory. Here, we pass the table name, column name only then we receive the cursor.

What is cursor class explain with example in Android?

Cursors are what contain the result set of a query made against a database in Android. The Cursor class has an API that allows an app to read (in a type-safe manner) the columns that were returned from the query as well as iterate over the rows of the result set.

Which function do you call from a cursor object to get the number of rows in the cursor object?

getCount. Returns the numbers of rows in the cursor.


2 Answers

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

like image 124
Nguyen Thanh Son Avatar answered Oct 17 '22 08:10

Nguyen Thanh Son


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.

like image 31
Dan Davis Avatar answered Oct 17 '22 07:10

Dan Davis