Jonathan
Jonathan

Reputation: 621

Android 3.0 Couldn't read row#, column# from cursor window

I have an application that runs fine on android 2.1, but when trying to transition it to 3.0 I get a cursor error that I'm not familar with.

Java.lang.IllegalStateException: Couldn't read row0, column -1 from cursor window. Make sure cursor is initialized correctly before accessing data from it.

All the data is storred in a SQLite database and this code works fine in android 2.1. Does a cursor have to be initialized differently in android 3.0?

Listed below is my code.

private void OpenGroupData(){
SQLiteDatabase db = openOrCreateDatabase(DATABASE_NAME,Context.MODE_PRIVATE,null);
Cursor cur = db.rawQuery("SELECT groupid FROM properties GROUP BY GroupID" + ";" , null);
LinearLayout glayout = (LinearLayout) findViewById(R.id.Grouplayout);
LinearLayout gwindow = (LinearLayout) findViewById(R.id.groupwindow);

TextView data = new TextView(this);
glayout.addView(data);
data.setText("");
int ID = cur.getColumnIndex("groupid");
int idvalue;

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);

try{
    // Check if our result was valid.
    cur.moveToFirst();
    if (cur != null) {

        // Loop through all Results
        do {data = new TextView(this);
            data.setTextSize(20);
        data.setClickable(true);
        data.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
              GroupClick(v);
            }
          });
        glayout.addView(data);
        idvalue = cur.getInt(ID);
        data.setId(idvalue);
        data.setText("Group: " + idvalue);
        }while(cur.moveToNext());
        } 
        cur.close();
        db.close();
        }   catch(Exception e) {
            Toast.makeText(getApplicationContext(), "Open Group Exception: " + e.toString(), Toast.LENGTH_SHORT).show();
        }
 }

Upvotes: 6

Views: 7312

Answers (5)

Kvant_hv
Kvant_hv

Reputation: 19

The -1 value returns form getColumnIndex(columnName) if 'columnName' can't be found. Check the column names

Upvotes: 0

vitez
vitez

Reputation: 83

-1 is the _id column which every sqlite table should have as is required by the android framework. If you have to add +1 to the index you are going wrong somewhere.

Upvotes: 1

jaanus
jaanus

Reputation: 99

If getColumnIndex returns -1, then the column doesn't exist. Otherwize it returnes zero-based column index.

Upvotes: 1

Bart
Bart

Reputation: 363

I ran into the same error message earlier this day. All it turned out to was that I made a typo in the column name. So, if it still applies, you could go and check the column name for typo's. Note that its case sensitive aswell. Error for me was along the lines of:

//Trew error
c.getColumnIndex("ArticleNumber");

//Was correct
c.getColumnIndex("Articlenumber");

Upvotes: 2

Jonathan
Jonathan

Reputation: 621

Alright I figured it out. For some reason when trying to transistion my application to 3.0 when my cursor goes and gets the column index for a field, in this case ("groupid") it is returning a value of -1. When the Cursor tries to start at -1 it crashes because it can't find a record at row(0), column(-1). So my fix was to just add one to the column index when getting the id. see below.

 int ID = cur.getColumnIndex("groupid") + 1;
 int idvalue;

By adding 1 to the Column index it seems to have solved the problem.

Upvotes: 1

Related Questions