android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 cursor -
i getting following error:
android.database.cursorindexoutofboundsexception: index 0 requested, size of 0.
from code:
public lirik getlirik(string id){ sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table_mupuh, new string[] { key_id, judul, key_mupuh }, key_id + "=?", new string[] { string.valueof(id) }, null, null, null); if (cursor != null && cursor.movetofirst()) cursor.movetofirst(); lirik lirik = new lirik(cursor.getstring(1), cursor.getstring(2)); db.close(); return lirik; }
and class viewmupuh
: error on : lirik lirik = db.getlirik(position);
public class viewmupuh extends activity implements onclicklistener { private static string position = null; private textview textjudul, textlirik; private button bupdate; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.detail_mupuh); intent intent = getintent(); position = intent.getstringextra("position"); if (position != null) { log.d("value of position", position); } dbhelper db = new dbhelper(this); lirik lirik = db.getlirik(position); textjudul = (textview) findviewbyid(r.id.judul_details); textjudul.settext(lirik.getjudul()); textlirik = (textview) findviewbyid(r.id.lirikdetails); textlirik.settext(lirik.getlirik()); bupdate = (button) findviewbyid(r.id.bupdatedetails); bupdate.setonclicklistener(this); } @override public void onclick(view v) { intent intent = new intent(getapplicationcontext(), editlirik.class); intent.putextra("position value", position); startactivity(intent); } }
you have several problems in code. first of all, calling cursor.movetofirst()
twice innecesarily:
if (cursor != null && cursor.movetofirst()) // <-- first call cursor.movetofirst(); // <-- second call
also, trying data cursor if cursor null or has retrieved no data:
if (cursor != null && cursor.movetofirst()) cursor.movetofirst(); lirik lirik = new lirik(cursor.getstring(1), cursor.getstring(2)); // <-- cursor can null here
anyway, leaving resources open. never close cursor
, if there problem, db
not closed either. suggest rewrite getlirik()
function way:
public lirik getlirik(final string id) { lirik lirik = null; sqlitedatabase db = null; cursor cursor = null; try { db = this.getreadabledatabase(); cursor = db.query(table_mupuh, new string[] { key_id, judul, key_mupuh }, key_id + "=?", new string[] { id }, null, null, null); if (cursor != null && cursor.movetofirst()) { lirik = new lirik(cursor.getstring(1), cursor.getstring(2)); } } catch (final exception e) { // exception (log, raise, ...) } { cursor.close(); db.close(); } return lirik; }
if so, take account can null when calling getlirik()
, so, in viewmupuh.oncreate()
should check avoid further errors:
textjudul = (textview) findviewbyid(r.id.judul_details); textlirik = (textview) findviewbyid(r.id.lirikdetails); dbhelper db = new dbhelper(this); lirik lirik = db.getlirik(position); if (lirik != null) { textjudul.settext(lirik.getjudul()); textlirik.settext(lirik.getlirik()); }
also take account hints point there no record in database matches id querying (maybe database empty?)
Comments
Post a Comment