Wei Chi
Wei Chi

Reputation: 29

Couldn't read row 0, col 2 from CursorWindow

i try to delete the data from listview by using the context menu and i get this java.lang.IllegalStateException: Couldn't read row 0, col 2 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

       public List<AssignmentRecord> getAllAssignment(){
    List<AssignmentRecord> records = new ArrayList<AssignmentRecord>();
    Cursor cursor = database.query(AssigmentContract.Assigment.TABLE_NAME, allColumn , null,
            null, null, null, null);
    cursor.moveToFirst();
    while(!cursor.isAfterLast()){
        AssignmentRecord assignmentRecord = new AssignmentRecord();
        assignmentRecord.setAssname(cursor.getString(0));
        assignmentRecord.setAssTime(cursor.getString(1));
        assignmentRecord.setAssID(cursor.getString(2));
        records.add(assignmentRecord);
        cursor.moveToNext();
    }

first line error assignmentRecord.setAssID(cursor.getString(2));

     dbcon.open();
    List<AssignmentRecord> records = dbcon.getAllAssignment();
    dbcon.close();

second error is List records = dbcon.getAllAssignment();

private static final String SQL_CREATE_ENTRIES = "CREATE TABLE "
        + AssigmentContract.Assigment.TABLE_NAME +
                "(" + AssigmentContract.Assigment.COLUMN_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                AssigmentContract.Assigment.COLUMN_ASS + " TEXT," +
                AssigmentContract.Assigment.COLUMN_ASSDATE+ " TEXT)";


       public final class AssigmentContract {
              public AssigmentContract(){}

public static abstract class Assigment implements BaseColumns{
    public static final String TABLE_NAME ="AssignmentV2";
    public static final String COLUMN_ASS ="AssignmentTitle";
    public static final String COLUMN_ASSDATE ="AssignmentDate";
    public static final String COLUMN_ID ="AssignmentID";
      }
       }

LOGCAT

     my.com.chyi.schedulev2 E/CursorWindow﹕ Failed to read row 0, column 2 from a CursorWindow which has 1 rows, 2 columns.
     my.com.chyi.schedulev2 D/AndroidRuntime﹕ Shutting down VM
           my.com.chyi.schedulev2 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x9e4f5908)
         /my.com.chyi.schedulev2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{my.com.chyi.schedulev2/my.com.chyi.schedulev2.assigmentActivity}: java.lang.IllegalStateException: Couldn't read row 0, col 2 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 2 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.database.CursorWindow.nativeGetString(Native Method)
        at android.database.CursorWindow.getString(CursorWindow.java:434)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
        at my.com.chyi.schedulev2.SQLController.getAllAssignment(SQLController.java:102)
        at my.com.chyi.schedulev2.assigmentActivity.onCreate(assigmentActivity.java:73)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)                 at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230 at android.app.ActivityThread.access$600(ActivityThread.java:141)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234 at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:137)at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:511)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)             at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 4192

Answers (2)

Apurva
Apurva

Reputation: 7901

Your getAllAssignment() should be as,

List<AssignmentRecord> records = new ArrayList<AssignmentRecord>();
SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery("select * from " + TABLE_NAME, null);

    if(cursor.moveToFirst()){
        do {
            AssignmentRecord assignmentRecord = new AssignmentRecord();
            assignmentRecord.setAssID(cursor.getString(0));
            assignmentRecord.setAssname(cursor.getString(1));
            assignmentRecord.setAssTime(cursor.getString(2));
        } while(cursor.moveToNext());
    }
    database.close();

Upvotes: 0

keshav kowshik
keshav kowshik

Reputation: 2374

Try this,

while(!cursor.isAfterLast()){
        AssignmentRecord assignmentRecord = new AssignmentRecord();
        assignmentRecord.setAssname(cursor.getString(cursor.getColumnIndex("your_respective_column_name));
        assignmentRecord.setAssTime(cursor.getString(cursor.getColumnIndex("your_respective_column_name)));
        assignmentRecord.setAssID(cursor.getString(cursor.getColumnIndex("your_respective_column_name)));
        records.add(assignmentRecord);
        cursor.moveToNext();

I have demonstrated what Harry suggested you to do. Here replace your_respective column name with the value you given for the columns which your trying to retrieve. For Ex: when you are trying to fetch the value of AssId it will be _id or some thing. Hope this helps you.

Upvotes: 0

Related Questions