Raju Gujarati Raju Gujarati - 5 months ago 136
Android Question

Android cursor Error : Make sure the Cursor is initialized correctly before accessing data from it

I am going to implement the method to retrieve the record of the strings but when it comes to the execution , it turns Runtime Error as the Log cat stated. Would you please tell us how in correctly initialise the cursor ?

Logcat

07-08 10:10:34.620: D/result(8036): true
07-08 10:10:34.660: D/memalloc(8036): ion: Unmapping buffer base:0x57c06000 size:466944
07-08 10:10:34.660: D/memalloc(8036): ion: Unmapping buffer base:0x57d6c000 size:466944
07-08 10:10:34.660: D/memalloc(8036): ion: Unmapping buffer base:0x57cbb000 size:466944
07-08 10:10:34.710: D/debug(8036): Order 87318702
07-08 10:10:34.710: D/pcDbHelper(8036): pcDbHelper com.example.recordandmovie.PhotoDbAdapter@412cad30
07-08 10:10:34.841: W/CursorWindow(8036): Window is full: requested allocation 1249663 bytes, free space 398939 bytes, window size 2097152 bytes
07-08 10:10:34.931: W/CursorWindow(8036): Window is full: requested allocation 1249663 bytes, free space 398939 bytes, window size 2097152 bytes
07-08 10:10:34.941: D/debug Cursor(8036): can be created
07-08 10:10:34.941: E/CursorWindow(8036): Failed to read row 0, column 1 from a CursorWindow which has 0 rows, 4 columns.
07-08 10:10:34.941: W/dalvikvm(8036): threadid=1: thread exiting with uncaught exception (group=0x40c051f8)
07-08 10:10:34.951: E/AndroidRuntime(8036): FATAL EXCEPTION: main
07-08 10:10:34.951: E/AndroidRuntime(8036): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.recordandmovie/com.example.recordandmovie.Main6}: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.os.Looper.loop(Looper.java:137)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.ActivityThread.main(ActivityThread.java:4519)
07-08 10:10:34.951: E/AndroidRuntime(8036): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 10:10:34.951: E/AndroidRuntime(8036): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 10:10:34.951: E/AndroidRuntime(8036): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
07-08 10:10:34.951: E/AndroidRuntime(8036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
07-08 10:10:34.951: E/AndroidRuntime(8036): at dalvik.system.NativeStart.main(Native Method)
07-08 10:10:34.951: E/AndroidRuntime(8036): 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.
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.database.CursorWindow.nativeGetString(Native Method)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.database.CursorWindow.getString(CursorWindow.java:450)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
07-08 10:10:34.951: E/AndroidRuntime(8036): at com.example.recordandmovie.Main6.onCreate(Main6.java:39)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.Activity.performCreate(Activity.java:4485)
07-08 10:10:34.951: E/AndroidRuntime(8036): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)


Below is my working

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;

public class Main6 extends Activity {

private PhotoDbAdapter pcDbHelper;
private String orderidGenerated;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);

Intent intent = getIntent();

if(intent!=null){
orderidGenerated = intent.getStringExtra("order");
}

Log.d("debug" , "Order " + orderidGenerated);

if(pcDbHelper ==null){
pcDbHelper = new PhotoDbAdapter(this);
pcDbHelper.open();
}

Log.d("pcDbHelper" , "pcDbHelper " + pcDbHelper);
Cursor cur = pcDbHelper.fetchOrderByRef(orderidGenerated);
if(cur!=null){
cur.moveToFirst();

Log.d("debug Cursor" , "can be created");
Log.d("KEY_PHOTO_HKID_HEX" , cur.getString(1));
Log.d("KEY_PHOTO_ADDRESS_HEX" , cur.getString(2));
Log.d("KEY_PHOTO_BANK_HEX" , cur.getString(3));
startManagingCursor(cur);

cur.close();
}else{
Log.d("debug Cursor" , "cannot be created");
}


}



}


public Cursor fetchOrderByRef(String refno) throws SQLException {

Cursor mCursor =

mDb.query(true, DATABASE_TABLE, new String[] {
KEY_REFNO ,
KEY_PHOTO_HKID_HEX ,
KEY_PHOTO_ADDRESS_HEX ,
KEY_PHOTO_BANK_HEX
},KEY_REFNO + "=" + refno,
null, null, null, null, null);

return mCursor;

}

Answer

The error message says

Failed to read row 0, column 1 from a CursorWindow which has 0 rows, 4 columns.

So your query is returning 0 rows. Your if statement should read:

if(cur!=null){
    // Best to manage any non-null cursor as then it will be closed when the
    // activity is stopped
    startManagingCursor(cur);
    // Only if a row was found
    if(cur.moveToFirst()) {
        Log.d("debug Cursor" , "can be created");
        Log.d("KEY_PHOTO_HKID_HEX" , cur.getString(1));
        Log.d("KEY_PHOTO_ADDRESS_HEX" , cur.getString(2));
        Log.d("KEY_PHOTO_BANK_HEX" , cur.getString(3));
    }else{
        // Handle no rows returned
    }
}else{
    Log.d("debug Cursor" , "cannot be created");
}