Naaz Naaz - 1 year ago 60
Android Question

Cursor DB not initialised although initialised

I have almost everything fine and quadruple checked every other thing related with the db and the cursor, I have a button in a activity upon clicking which the following code runs to access the string from a public function in other class:

DatabaseHandler dbfunc = new DatabaseHandler(getApplicationContext());
String deviceCODE = dbfunc.getUSERdetailsSTRING();
Log.i(TAG, deviceCODE);

The following is the public function in DB handler class:

public String getUSERdetailsSTRING(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
if(cursor.getCount() > 0){
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("uid", cursor.getString(3));
user.put("created_at", cursor.getString(4));
user.put("Device Code", cursor.getString(5));
String deviceCODE = user.get(KEY_UID);
return deviceCODE;

BUT it shows me the following error on logcat:

08-16 22:06:24.840: E/AndroidRuntime(25923): FATAL EXCEPTION: main
08-16 22:06:24.840: E/AndroidRuntime(25923): java.lang.IllegalStateException: Couldn't read row 0, col 5 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
08-16 22:06:24.840: E/AndroidRuntime(25923): at android.database.CursorWindow.nativeGetString(Native Method)
08-16 22:06:24.840: E/AndroidRuntime(25923): at android.database.CursorWindow.getString(
08-16 22:06:24.840: E/AndroidRuntime(25923): at android.database.AbstractWindowedCursor.getString(

Answer Source

The place from where I took the example( that guy started the counting from cursor.getString(1); from ONE However it should be started with a ZERO '0', its solved now!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download