Search code examples
javaandroidsqliteandroid-studioandroid-sqlite

Error With Cursor When Loading Details From Database


I'm currently working through a school project, trying to create a Scout Group manager. This is my first time using Java, XML or Android Studio, so progress has been a little slow, but I'm getting there. The issue I'm having at the moment is with extracting Scout details from a database, in order for them to be placed in a spinner. The leader will then select the Scouts they want to invite, by name, from the spinner. The code which is apparently causing the error (in the database handler class) is below:

public Cursor GetAllScouts()
{
    String whereClause = COLUMN_SECTION + "=?";
    String[] whereArgs = new String[]{"Beavers","Cubs","Scouts","Explorers","Network","Leaders"};
    String[] columns = new String[]{COLUMN_SCID + " AS " + BaseColumns._ID, COLUMN_FNAME, COLUMN_LNAME, COLUMN_SECTION};
    return db.query(TABLE_SCOUTS,columns,whereClause,whereArgs,null,null,null);//Error is being thrown up here
}

And the information from the logcat is below again:

01-24 18:39:38.228 18179-18179/com.example.atomi.scoutmanagerprototype E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.atomi.scoutmanagerprototype, PID: 18179
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.atomi.scoutmanagerprototype/com.example.atomi.scoutmanagerprototype.frmCreateEvent2}: java.lang.IllegalArgumentException: Cannot bind argument at index 6 because the index is out of range.  The statement has 1 parameters.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
    at android.app.ActivityThread.access$800(ActivityThread.java:148)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5310)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 6 because the index is out of range.  The statement has 1 parameters.
    at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212)
    at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166)
    at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
    at com.example.atomi.scoutmanagerprototype.databaseHandler.GetAllScouts(databaseHandler.java:395)
    at com.example.atomi.scoutmanagerprototype.frmCreateEvent2.loadSpinnerData(frmCreateEvent2.java:87)
    at com.example.atomi.scoutmanagerprototype.frmCreateEvent2.onCreate(frmCreateEvent2.java:82)
    at android.app.Activity.performCreate(Activity.java:5953)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386) 
    at android.app.ActivityThread.access$800(ActivityThread.java:148) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5310) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 

Any help would be appreciated. Thanks in advance.


Solution

  • You should check out this answer. Your selection (where clause) must contain exactly as many ? as arguments are given in your selection arguments. In this case your selection only contains one ? but you give 6 selection arguments.

    The selection you probably want is:

    COLUMN_SECTION + "IN (?, ?, ?, ?, ?, ?)"
    

    Passing your list of section as the selectionArgs should work then.

    Consider checking out this tutorial to learn more about saving data with SQLite.