Can't read row 0, col -1 from CursorWindow

Hi all, I’m getting a crash in this chapter (page 293) when I’m trying to run the app after Listing 15.6 on a physical Google Pixel phone.

There is a paragraph stating to uninstall and click run in Android Studio after I add columns, which I’ve done, but I still get the following error:

FATAL EXCEPTION: main
Process: com.bignerdranch.android.criminalintent, PID: 15579
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.criminalintent/com.bignerdranch.android.criminalintent.CrimeListActivity}: java.lang.IllegalStateException: Couldn’t read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

Please note I get this error in the pre-completed chapter too.

How can I resolve this in order to move on? Thank you kindly.

This line in the error: Couldn't read row 0, col -1 says that you are trying to read from column index -1, which is an invalid index.

In the code, we use cursor.getColumnIndex("...") to find the index of a column in the result set of a given name. If that column isn’t there, this method will return -1. So, this means your column isn’t in the results.

Usually, this is because of an issue with the create table sql statement. Take a look at your sql statement that you use to create the crimes table. Post the code here if you like and I can check it out too.

Thanks cstewart.

I’ve successfully run it on an emulator, but on a hardware Pixel it’s not uninstalling the previous database. I uninstalled, crashed on open with the posted error, so I then force stopped / cleared all data / then uninstalled and installed the new one. Same -1 crash. I went back and reinstalled chapter 14 code and it somehow shows the SQL test data I’d entered in chapter 14 despite all my uninstalls! So it’s not getting rid of the database no matter what I do on a Pixel - it persists, and because of the extra column in the new code it crashes. :confused:

My create code is below:

package com.bignerdranch.android.criminalintent.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.bignerdranch.android.criminalintent.database.CrimeDbSchema.CrimeTable;

public class CrimeBaseHelper extends SQLiteOpenHelper {
    private static final int VERSION = 1;
    private static final String DATABASE_NAME = "crimeBase.db";

    public CrimeBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + CrimeTable.NAME + "(" +
        " _id integer primary key autoincrement, " +
        CrimeTable.Cols.UUID + ", " +
        CrimeTable.Cols.TITLE + ", " +
        CrimeTable.Cols.DATE + ", " +
        CrimeTable.Cols.SOLVED + ", " +
        CrimeTable.Cols.SUSPECT +
        ")"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

I’ve just resolved this. Apparently in Android 6.0 (API 23) or higher, apps now automatically participate in auto backup. In AndroidManifest.xml change the following line from true to false:

<application ...
    android:allowBackup="true">
</app>

This will stop Google Drive from squirrelling the database to it’s drive and restoring it every time you reinstall the app.

Ah! Great catch. I’ve had trouble with this exact thing on projects in the past. Thank you for posting the followup.

Same problem here. Tried change [quote=“Tzar, post:4, topic:11995”]
<application …
android:allowBackup=“true”>
</app>
[/quote]

no sucess. Code straight from source, no changes made. Any ideas?

change android:allowBackup to ‘false’, then uninstall the app and reinstall the app on your device.

thank you so much for this. a huge help.