-1

so here's my case: while trying to make a database, i encountered a serious issue that is killing me! so here's the code:

}public ArrayList<String> ListProduits() {
        ArrayList<String> productList= new ArrayList<>(  );
        SQLiteDatabase ProductDatabase = this.getReadableDatabase();
        try {
            String querySelector = "SELECT * FROM " + TABLENAME;
            Cursor queryCursor = ProductDatabase.rawQuery(querySelector, null);
            if (queryCursor.getCount()>0) {
                while (queryCursor.moveToNext()) {
                    String product = queryCursor.getString(queryCursor.getColumnIndex("products"));
                    productList.add(product);
                }
            }ProductDatabase.setTransactionSuccessful();
        } catch ( Exception e) {
            e.printStackTrace();
        }
return productList;
    }

and it returned me this:

`Value must be ≥ 0

can anyone explain this?

btw everything else works fine

Zain
  • 37,492
  • 7
  • 60
  • 84
  • according to the Android Developers reference, `getColumnIndex (String columnName) : Returns the zero-based index for the given column name, or -1 if the column doesn't exist.` , this only means that you didn't provide your column name correctly, check your column name if it matches what you're passing. - https://developer.android.com/reference/android/database/Cursor#getColumnIndex(java.lang.String) – Omar Shawky Oct 04 '21 at 23:58
  • use `queryCursor.getColumnIndexOrThrow(..)` instead - https://stackoverflow.com/q/69053061/4252352 see comments – Mark Oct 05 '21 at 00:20

2 Answers2

0

I think this might be a bug with SDK for 31.

Consider the following :- (screen shot to show flagged code) :-

enter image description here

Embedding/invoking the getColumnIndex within the getString results in the Value must be ≥ 0 flag/error.

BUT using csr.getColumnIndex on it's own is fine for EXACTLY the same.

If the build gradle is altered from :-

....
android {
    compileSdk 31

    defaultConfig {
        applicationId "the_package"
        minSdk 21
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
....
  • note this was an attempt to see if reducing just the target version overcame the issue. It didn't.

to :-

android {
    compileSdk 30

    defaultConfig {
        applicationId "the_package"
        minSdk 21
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
....

and resynced then :-

enter image description here

Fix

use one of the following:-

  • change the build gradle from 31 down to 30 or
  • get the column index separately (e.g. like as in the code displayed).
  • Suppress the warning e.g. @SuppressLint("Range") String getIt = csr.getString(csr.getColumnIndex("A COLUMN"));)
  • Ignore the error and run anyway
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • thansk, found i made a beginner mistake – Quentin Ramecourt Oct 06 '21 at 17:29
  • @QuentinRamecourt no mistake on your behalf, there is something wrong with how LINT is interpreting the code. It says that the value is out of range when it can't know that it will be but only for getColumnIndex when it is embedded if a get????. The fixes just get around the bug. If the answer helped you then you may wish to tick the answer so others will know that the answer helped. – MikeT Oct 06 '21 at 18:56
  • no it was me who had forgotten a few lines, i assure. after adding them it worked fine. – Quentin Ramecourt Oct 06 '21 at 20:25
0

Use getColumnIndexOrThrow instead of getColumnIndex

getColumnIndex Returns the zero-based index for the given column name, or -1 if the column doesn't exist. If you expect the column to exist use getColumnIndexOrThrow(java.lang.String) instead, which will make the error more clear.

reference : https://developer.android.com/reference/android/database/Cursor#getColumnIndex(java.lang.String)

akshay
  • 11
  • 4