sqlite - Android cursor always returning null -


i keep getting following error

android.database.cursorindexoutofboundsexception: index -1 requested, size of 1 

i know have data in database because similar query data (rather single entry) pulls out list successfully

here code database helper

public class databasehandler extends sqliteopenhelper {     ...      public service getmostrecentservice() {     sqlitedatabase db = this.getreadabledatabase();     string selectquery = "select  * " + table_contacts + " order " + key_id + " desc limit 1;";      cursor cursor = db.rawquery(selectquery, null);      service service = new service();     if(cursor != null) {         service = new service(cursor.getstring(1), cursor.getstring(2));     }      return service;     } } 

any ideas why keep getting out of bounds exception?

you have not positioned cursor on row. initially, @ position -1. presumably, given existing code, should call movetofirst() on cursor after null check , before getstring() calls.


Comments