skip to Main Content

I’m a beginner that trying to build an health app for my school project

The program runs fine when I use Android 11 (API 30). The error happened when I use Android higher than 11 (API 30+).

The code where error happened:

private fun refreshMe(){
    val helper = profileHelper(applicationContext)
    val db = helper.readableDatabase
    val rs = db.rawQuery("SELECT * FROM uProfile", null)

    rs.moveToLast()
    nama.text = rs.getString(1) //THE ERROR HAPPEN HERE
    umur.text = rs.getString(2)
    jk.text = rs.getString(3)
    data.text = rs.getString(4)
    kadarAu.text = rs.getString(5)
    kadarGd.text = rs.getString(6)
    kadarHb.text = rs.getString(7)
    kadarCh.text = rs.getString(8)
    kadarTdSis.text = rs.getString(9)
    kadarTdDia.text = rs.getString(10)
    rs.close()
    }

The error log

FATAL EXCEPTION: main

Process: com.lhsproj.labhealthpocket, PID: 10098
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lhsproj.labhealthpocket/com.lhsproj.labhealthpocket.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:205)
    at android.os.Looper.loop(Looper.java:294)
    at android.app.ActivityThread.main(ActivityThread.java:8177)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:521)
    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:139)
    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:53)
    at com.lhsproj.labhealthpocket.MainActivity.refreshMe(MainActivity.kt:63)
    at com.lhsproj.labhealthpocket.MainActivity.onCreate(MainActivity.kt:251)
    at android.app.Activity.performCreate(Activity.java:8595)
    at android.app.Activity.performCreate(Activity.java:8573)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3764)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loopOnce(Looper.java:205) 
    at android.os.Looper.loop(Looper.java:294) 
    at android.app.ActivityThread.main(ActivityThread.java:8177) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) 

I’ve tried several solution from Stack Overflow earlier, but I found that they were difficult to implement in my project.

2

Answers


  1. android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0

    Your cursor is null. Check if it’s null or not.

    Login or Signup to reply.
  2. You should ALWAYS check if a move (in your case moveToLast) was successful. all the move methods return a Boolean, true if the move was successful or false if the move could not be made.

    Hard coding cursor index values is typically not recommended. Instead it is recommended to typically use the Cursor’s getColumnIndex or getColumnIndexOrThrow method to ascertain the index for a column and it’s validity (-1 if invalid column name or IllegalArgumentException for a throw).

    See Cursor

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search