0

I'm using a SQLite database to store some data. When retrieving I get an exception. I have no clue what i'm doing wrong. That's why i'm asking this question.

Database code:

//Creating the database
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE "+garbageTable+" (" +colID+ " INTEGER PRIMARY KEY AUTOINCREMENT , " +colName+ " TEXT, "+colInterval+ " INTEGER, " +colDate+ " INTEGER, " +colImage+ " INTEGER)");
}

//Insert some data
public void insertGarbageObject(GarbageObject g){
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues cv=new ContentValues();

    cv.put(colName, g.name);
    cv.put(colInterval, g.dayInterval);
    cv.put(colDate, g.date.getTimeInMillis());
    cv.put(colImage, g.image);
    db.insert(garbageTable, colID, cv);

    db.close(); 
}

//Read all inserted data
public ArrayList<GarbageObject> getGarbageObjects(){
    ArrayList<GarbageObject> garbageObjects = new ArrayList<GarbageObject>();

    SQLiteDatabase db=this.getReadableDatabase();
    Cursor cur=db.rawQuery("SELECT * FROM "+garbageTable,new String [] {});
    for(int i = 0; i<cur.getCount(); i++){
        GarbageObject g = new GarbageObject(c.getString(R.string.naam));
        g.ID = cur.getInt(cur.getColumnIndexOrThrow(colID));//Exception occurs!
        g.name = cur.getString(cur.getColumnIndexOrThrow(colName));//Exception occurs!
        g.dayInterval = cur.getInt(cur.getColumnIndexOrThrow(colInterval));//Exception occurs!
        g.date.setTimeInMillis(cur.getLong(cur.getColumnIndexOrThrow(colDate)));//Exception occurs!
        g.image = cur.getInt(cur.getColumnIndexOrThrow(colImage));//Exception occurs!


        garbageObjects.add(g);
    }
    cur.close();
    db.close();

    return garbageObjects;
}

My logcat:

10-14 17:35:58.356: W/dalvikvm(18570): threadid=1: thread exiting with uncaught exception (group=0x40f27680)
10-14 17:35:58.356: E/AndroidRuntime(18570): FATAL EXCEPTION: main
10-14 17:35:58.356: E/AndroidRuntime(18570): java.lang.RuntimeException: Unable to start activity ComponentInfo{robin.garbagereminder/robin.activities.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
10-14 17:35:58.356: E/AndroidRuntime(18570):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at android.app.ActivityThread.access$600(ActivityThread.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at android.os.Handler.dispatchMessage(Handler.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at android.os.Looper.loop(Looper.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at android.app.ActivityThread.main(ActivityThread.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at java.lang.reflect.Method.invokeNative(Native Method)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at java.lang.reflect.Method.invoke(Method.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at dalvik.system.NativeStart.main(Native Method)
10-14 17:35:58.356: E/AndroidRuntime(18570): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
10-14 17:35:58.356: E/AndroidRuntime(18570):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at robin.garbagereminder.DatabaseHelper.getGarbageObjects(DatabaseHelper.java:59)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at robin.activities.MainActivity.init(MainActivity.java:88)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at robin.activities.MainActivity.onCreate(MainActivity.java:33)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at android.app.Activity.performCreate(Activity.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
10-14 17:35:58.356: E/AndroidRuntime(18570):    ... 12 more
1
  • FYI, logcat is better as a code block than a blockquote. I edited the Question for you. Commented Oct 14, 2014 at 17:40

1 Answer 1

1

You don't use the SQliteCursor correctly, it works this way:

if (cur != null ) {
    if  (cur.moveToFirst()) {
        do {
            String tmp = c.getString(R.string.naam)
            ...
        }while (cur.moveToNext());
    }
}

In your Example you do nothing with the Cursor Object, and also nothing with the Cursor. Adding cur.moveToNext() at the beginning of the for-Loop will be exactly the same, but cleaner version is the while-loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate, I indeed forgot cur.moveToPosition(i);

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.