0

I'm new in android development and am getting this errors when click my "getir" button. It error after work. How to solve this issue

my code code:

public  void getir(View view)
{
cursor = myData.rawQuery("SELECT * FROM kolonVeri", null);
    int gunIx = cursor.getColumnIndex("gun");
    int vtarihIx = cursor.getColumnIndex("vTarih");
        cursor.moveToFirst();

        while (cursor != null)
        {
         System.out.println("VARDİYA: " + cursor.getString(gunIx));
         System.out.println("TARİH: " + cursor.getString(vtarihIx));
            cursor.moveToNext();
        }

}

error log

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hakannacar.vardiyam, PID: 19541
java.lang.IllegalStateException: Could not execute method for android:onClick
    om.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.reflect.InvocationTargetException
   com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
 Caused by: android.database.CursorIndexOutOfBoundsException: Index 4 requested, with a size of 4

2 Answers 2

1

A returned Cursor (e.g. as from rawQuery) will not be null, the check for null is useless and as shown in this case dangerous.

Rather you should be checking the Cursor move??? methods to see if they return true.

The actual issue is that you end up trying to retrieve data for the 5th row (Index 4) when there are only 4 rows (size), as the check for != null will always be true, so the loop continues beyond the 4th row.

As such your code could be :-

public  void getir(View view)
{
    cursor = myData.rawQuery("SELECT * FROM kolonVeri", null);
    int gunIx = cursor.getColumnIndex("gun");
    int vtarihIx = cursor.getColumnIndex("vTarih");
    while (cursor.moveToNext()) {
         System.out.println("VARDİYA: " + cursor.getString(gunIx));
         System.out.println("TARİH: " + cursor.getString(vtarihIx));
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

A returned cursor can be null. The check where he has it is useless, as by then he's already derefeneced it.
@GabeSechan well bless my little white cotton sokcs. I never realised that by dereferncing a cursor (however that was done) still allows acces to the cursor to get Caused by: android.database.CursorIndexOutOfBoundsException: Index 4 requested, with a size of 4. you learn something everyday I guess.
0

You have to check if the cursor has more items, replace your while loop with this

if (cursor.moveToFirst()) {
    do {
       System.out.println("VARDİYA: " + cursor.getString(gunIx));
       System.out.println("TARİH: " + cursor.getString(vtarihIx));            
    } while (cursor.moveToNext());
}

1 Comment

This would fail with the same issue if the Cursor were empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.