java - android.database.CursorIndexOutOfBoundsException: No idea why -


i recieved error report user using android application.

java.lang.runtimeexception: unable start activity componentinfo{c`om.gurpswu.gurps/com.gurpswu.gurps.home}: android.database.cursorindexoutofboundsexception: index 0 requested, size of 0 @ android.app.activitythread.performlaunchactivity(activitythread.java:2669) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2685) @ android.app.activitythread.access$2300(activitythread.java:126) @ android.app.activitythread$h.handlemessage(activitythread.java:2038) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:123) @ android.app.activitythread.main(activitythread.java:4633) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:521) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:858) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) @ dalvik.system.nativestart.main(native method) caused by: android.database.cursorindexoutofboundsexception: index 0 requested, size of 0 @ android.database.abstractcursor.checkposition(abstractcursor.java:580) @ android.database.abstractwindowedcursor.checkposition(abstractwindowedcursor.java:214) @ android.database.abstractwindowedcursor.getint(abstractwindowedcursor.java:84) @ com.gurpswu.gurps.player.getintfield(player.java:137) @ com.gurpswu.gurps.home.hudsetup(home.java:102) @ com.gurpswu.gurps.home.oncreate(home.java:25) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) @ android.app.activitythread.performlaunchactivity(activitythread.java:2633) ... 11 more` 

i haven't been able recreate problem. hope can me. code on home screen retrieves values sqlite database.

 public string getstringfield(string fieldname) { string[] columns = new string[] { key_rowid, key_name, key_city, key_health, key_energy, key_rank, key_cash, key_balance, key_donated }; cursor c = ourdatabase.query(true, database_table, columns, null, null, null, null, "_id desc", "1"); if (c != null) { c.movetofirst(); string name = c.getstring(c.getcolumnindexorthrow(fieldname)); c.close(); return name; } return null; } public int getintfield(string fieldname) { string[] columns = new string[] { key_rowid, key_name, key_city, key_health, key_energy, key_rank, key_cash, key_balance, key_donated }; cursor c = ourdatabase.query(true, database_table, columns, null, null, null, null, "_id desc", "1"); if (c != null) { c.movetofirst(); int result = c.getint(c.getcolumnindexorthrow(fieldname)); c.close(); return result; } return (integer) null; } 

in stack trace, first line have cursorindexoutofboundsexception: index 0 requested, size of 0. check c.getcount() before c.movetofirst():

if(c != null) { try { if (c.getcount() > 0) { c.movetofirst(); return c.getint(c.getcolumnindexorthrow(fieldname)); } } { c.close(); } } 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -