1

I'm having a problem retrieving data, it says make sure cursor is initialized correctly before accessing data from it i can't figure out the problem my cursor is initialized properly,

My logcat below.

      02-16 20:34:49.678 22333-22333/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.computer.mathkiddofinal:second, PID: 22333
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.computer.mathkiddofinal/com.example.computer.mathkiddofinal.Database.Record_DB}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                       at android.os.Handler.dispatchMessage(Handler.java:110)
                                                       at android.os.Looper.loop(Looper.java:193)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
                                                       at dalvik.system.NativeStart.main(Native Method)
                                                    Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                       at android.database.CursorWindow.nativeGetLong(Native Method)
                                                       at android.database.CursorWindow.getLong(CursorWindow.java:507)
                                                       at android.database.CursorWindow.getInt(CursorWindow.java:574)
                                                       at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:80)
                                                       at com.example.computer.mathkiddofinal.Database.Record_DB.onCreate(Record_DB.java:55)
                                                       at android.app.Activity.performCreate(Activity.java:5264)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                       at android.os.Looper.loop(Looper.java:193) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                       at java.lang.reflect.Method.invokeNative(Native Method) 

Record_DB.java, error in this line int postwh = res.getInt(res.getColumnIndex("POSTWH"));

public class Record_DB extends Activity {
Final_Result_Grade_4 asd;
TextView txtDB;
    DatabaseHelper myDb;
    ArrayList<String> results = new ArrayList<String>();
    ArrayList<Integer> lpostwh = new ArrayList<Integer>();
    ArrayList<Integer> lpostmul = new ArrayList<Integer>();
    ArrayList<Integer> lpostdiv = new ArrayList<Integer>();
    ArrayList<Integer> lprewh = new ArrayList<Integer>();
    ArrayList<Integer> lpremul = new ArrayList<Integer>();
    ArrayList<Integer> lprediv = new ArrayList<Integer>();
    ListView errorListview;
    ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record__db);
        errorListview = (ListView) findViewById(R.id.listtt);
        //TextView txtDB = (TextView) findViewById(R.id.txtDB);
       // txtDB.setMovementMethod(new ScrollingMovementMethod());
        myDb = new DatabaseHelper(this);
        Cursor res = myDb.getAllData();
        int cols = res.getColumnCount();
        if (res.getCount() == 0) {
            Toast.makeText(Record_DB.this, "no data", Toast.LENGTH_SHORT).show();
            return;
        }

        while (res.moveToNext()) {
            int id = res.getInt(res.getColumnIndex("ID"));
            String name = res.getString(res.getColumnIndex("NAME"));
            int pre = res.getInt(res.getColumnIndex("PRETEST"));
            int post = res.getInt(res.getColumnIndex("POSTTEST"));
            int postwh = res.getInt(res.getColumnIndex("POSTWH"));
            int postmul = res.getInt(res.getColumnIndex("POSTMUL"));
            int postdiv = res.getInt(res.getColumnIndex("POSTDIV"));
            int prewh = res.getInt(res.getColumnIndex("PREWH"));
            int premul = res.getInt(res.getColumnIndex("PREMUL"));
            int prediv = res.getInt(res.getColumnIndex("PREDIV"));
            results.add(""+postwh + postmul + postdiv + prewh + premul + prediv );


        }

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, results);
        errorListview.setAdapter(adapter);

I can't really figure out how to solve this problem, anyone can help. really appreciate thanks

DataBaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "gradefour.db";
    public static final String TABLE_NAME = "grade_four_table";
    public static final String COL_1= "ID";
    public static final String COL_2= "NAME";
    public static final String COL_3= "PRETEST";
    public static final String COL_4= "POSTTEST";
    public static final String COL_5= "POSTWH";
    public static final String COL_6= "POSTMUL";
    public static final String COL_7= "POSTDIV";
    public static final String COL_8= "PREWH";
    public static final String COL_9= "PREMUL";
    public static final String COL_10= "PREDIV";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PRETEST INTEGER,POSTTEST INTEGER,POSTWH INTEGER,POSTMUL INTEGER,POSTDIV INTEGER,PREWH INTEGER,PREMUL INTEGER,PREDIV INTEGER) ");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    }
    public boolean inserData(String name, Integer pretest, Integer posttest, Integer postwh, Integer postmul, Integer postdiv,
                             Integer prewh, Integer premul, Integer prediv){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,pretest);
        contentValues.put(COL_4,posttest);
        contentValues.put(COL_5,postwh);
        contentValues.put(COL_6,postmul);
        contentValues.put(COL_7,postdiv);
        contentValues.put(COL_8,prewh);
        contentValues.put(COL_9,premul);
        contentValues.put(COL_10,prediv);
       long result =  db.insert(TABLE_NAME,null,contentValues);
       if(result==-1){
           return false;
       }
        else
       {
           return true;
       }
    }
    public Cursor getAllData(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);

        return res;
    }
}
14
  • please provide the code from Your database helper class.... Commented Feb 16, 2016 at 12:47
  • @Lynerd: Plz post your DB create table query Commented Feb 16, 2016 at 12:47
  • 1
    Are you sure that the table exists and that the cursor is not null? You should check it with if(cursor != null && cursor.moveToFirst()). Then you should check with cursor.moveToFirst() Commented Feb 16, 2016 at 12:49
  • 1
    You need to add space between comma and next string Commented Feb 16, 2016 at 12:53
  • 1
    @Lynerd: add ';' at the end of create table query" ".... PREDIV INTEGER) ;"); Commented Feb 16, 2016 at 12:57

3 Answers 3

1

As Per your comment..!!

You have add some 6 columns after installing your app. So you have to need reinstall app then after new table is created and 6 columns will be added in your table.

Simply your OnCreate() method called only when your application is installed and at that time your table is created, so when you have to need some changes then you have two options

  1. Make changes in OnUpgrade() method and Update your app. (Do for live applications)

  2. Uninstall app and again install it. (Do for testing)

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

Comments

1

Before reading the cursor do

res.moveToFirst();

2 Comments

do i need to enclose my while loop in if(res.moveToFirst())
You don't need to enclose res.moveToFirst() in an if statement, do res.moveToFirst() before the while loop, for example, after you created
1

Try to iterate on your cursor like the following example and dont forget to close your database as well as your cursor object.

if(cursor.moveToFirst()){
   ...
}
db.close();
cursor.close();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.