0

I have an SQLite database and I'm trying to populate some TextViews with it. After the database is successfully populated, I do a query like this:

public Cursor getSubject(String dan, int ura, String oddelek){
    String[] columns = new String[]{SQLiteHelper.PREDMET};
    String selection = SQLiteHelper.DAN+"=? and "+SQLiteHelper.URA+"=? and "+SQLiteHelper.ODDELEK+"=?";
    String[] selectionArgs = new String[]{dan, Integer.toString(ura), oddelek};
    open();
    return db.query(
            SQLiteHelper.IME_TABELE, 
            columns, 
            selection, 
            selectionArgs,
            null, null, null);
}

This is the code when populating TextViews:

Cursor c = dbHandler.getSubject(dan, ura, razred);
    if (!c.moveToFirst())
    {
        tw.setText("");
    }
    else {
        String predmet = c.getString(c.getColumnIndex(SQLiteHelper.PREDMET));
        String prostor = c.getString(c.getColumnIndex(SQLiteHelper.UCILNICA));
        tw.setText(predmet);
        ucilnica.setText(prostor);
    }

So as you can see, I simply do a query, then check if cursor can find a first row and after it finds it, I populate TextViews. The weird thing is that it works only for "PREDMET" and not for "UCILNICA". App crashes when it gets to "UCILNICA".

Logcat that I get:

01-14 17:14:54.381: E/AndroidRuntime(3862): FATAL EXCEPTION: main
01-14 17:14:54.381: E/AndroidRuntime(3862): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
01-14 17:14:54.381: E/AndroidRuntime(3862):     at android.database.CursorWindow.nativeGetString(Native Method)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at android.database.CursorWindow.getString(CursorWindow.java:434)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at com.whizzapps.stpsurniki.Schedule.populateUrnik(Schedule.java:76)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at com.whizzapps.stpsurniki.Schedule$PopulateDatabase.onPostExecute(Schedule.java:218)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at com.whizzapps.stpsurniki.Schedule$PopulateDatabase.onPostExecute(Schedule.java:1)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at android.os.AsyncTask.finish(AsyncTask.java:631)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at android.os.Looper.loop(Looper.java:137)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at java.lang.reflect.Method.invokeNative(Native Method)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at java.lang.reflect.Method.invoke(Method.java:525)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-14 17:14:54.381: E/AndroidRuntime(3862):     at dalvik.system.NativeStart.main(Native Method)

So according to logcat, the index of "UCILNICA" isn't right, if I'm correct? But why isn't it? As far as I know I'm doing everything right. The funny thing is that "PREDMET" and "UCILNICA" are both in the same row and both populated, so it just doesn't make sense to me that one works and one doesn't.

1 Answer 1

2

I think it's because your missing the ULCINICA column from your columns your passing into the query. At the moment you are only selecing PREDMET. Try changing.

String[] columns = new String[]{SQLiteHelper.PREDMET};

to

String[] columns = new String[]{SQLiteHelper.PREDMET, SQLiteHelper.UCILNICA};
Sign up to request clarification or add additional context in comments.

Comments

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.