android.database.CursorIndexOutOfBoundsException: Index 8 requested, with a size of 8

i am getting the following problem any help will be very helpful (I always upvote) :

for some reason my code is logging what i believe should be done in two sets is done in six sets plus giving me an error.

here is my code :

  package com.nxstudio.suratbus;

    import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * Created by rahul on 7/5/2016.
 */
public class DBHelper {

public void insert_coordinates(Context context){

    try {

        SQLiteDatabase eventsDB = context.openOrCreateDatabase("Coordinates", Context.MODE_PRIVATE, null);

        eventsDB.execSQL("CREATE TABLE IF NOT EXISTS coordinates (latitude VARCHAR,longitude VARCHAR,station VARCHAR)");

        eventsDB.execSQL("INSERT INTO coordinates (latitude, longitude ,station) VALUES ('1', '1','Station1')");

        eventsDB.execSQL("INSERT INTO coordinates (latitude, longitude ,station) VALUES ('2', '2','Station2')");

        Cursor c = eventsDB.rawQuery("SELECT * FROM coordinates", null);

        int latIndex = c.getColumnIndex("latitude");
        int longIndex = c.getColumnIndex("longitude");
        int stationIndex = c.getColumnIndex("station");

        if (c != null && c.moveToFirst()){

        while (c != null) {

            Log.i("lat", c.getString(latIndex));
            Log.i("long", Integer.toString(c.getInt(longIndex)));
            Log.i("Station", Integer.toString(c.getInt(stationIndex)));

            c.moveToNext();
        }
    }


    }
    catch (Exception e) {

        e.printStackTrace();

    }



}




}

here is my log :

    07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/lat: 1
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/long: 1
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/Station: 0
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/lat: 2
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/long: 2
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/Station: 0
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/lat: 1
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/long: 1
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/Station: 0
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/lat: 2
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/long: 2
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/Station: 0
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/lat: 1
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/long: 1
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/Station: 0
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/lat: 2
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/long: 2
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/Station: 0
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/lat: 1
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/long: 1
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/Station: 0
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/lat: 2
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/long: 2
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus I/Station: 0
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err: android.database.CursorIndexOutOfBoundsException: Index 8 requested, with a size of 8
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:432)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at com.nxstudio.suratbus.DBHelper$override.insert_coordinates(DBHelper.java:35)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at com.nxstudio.suratbus.DBHelper$override.access$dispatch(DBHelper.java)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at com.nxstudio.suratbus.DBHelper.insert_coordinates(DBHelper.java:0)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at com.nxstudio.suratbus.MainActivity.onCreate(MainActivity.java:75)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at android.app.Activity.performCreate(Activity.java:5245)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at android.app.ActivityThread.access$900(ActivityThread.java:141)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at android.os.Looper.loop(Looper.java:136)
07-05 14:38:05.512 20886-20886/com.nxstudio.suratbus W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5052)
07-05 14:38:05.522 20886-20886/com.nxstudio.suratbus W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 14:38:05.522 20886-20886/com.nxstudio.suratbus W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
07-05 14:38:05.522 20886-20886/com.nxstudio.suratbus W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-05 14:38:05.522 20886-20886/com.nxstudio.suratbus W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
07-05 14:38:05.522 20886-20886/com.nxstudio.suratbus W/System.err:     at dalvik.system.NativeStart.main(Native Method)

Thanks in advance

Answer

To fix the crash, replace

while (c != null) {
    //...
    c.moveToNext();
}

with e.g.

do {
     //...
} while (c.moveToNext());

so that your loop terminates when there are no more results.

For the “two sets is done in six sets” issue, you can add DROP TABLE IF EXISTS coordinates before the CREATE TABLE to remove the old table with older data.

Leave a Reply

Your email address will not be published. Required fields are marked *