java - CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 - Database Android -
i've experienced error in program. i'm trying make quiz app using radiobutton. question , answer option obtained database.
this part of code:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); /* intent parameter */ /* open database file */ db_helper.opendatabase(); db = db_helper.getreadabledatabase(); this.requestwindowfeature(window.feature_no_title); this.setcontentview(r.layout.activity_exercise_question); qcontent = (textview) findviewbyid(r.id.txtquestion); qnum = (textview) findviewbyid(r.id.txtnumb); = (radiobutton) findviewbyid(r.id.rba); b = (radiobutton) findviewbyid(r.id.rbb); c = (radiobutton) findviewbyid(r.id.rbc); d = (radiobutton) findviewbyid(r.id.rbd); g = (radiogroup) findviewbyid(r.id.rgoption); next = (button) findviewbyid(r.id.btnnextquestion); next.setbackgroundcolor(color.green); //to handle event when button next clicked next.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub int radiobuttonid = g.getcheckedradiobuttonid(); view radiobutton = g.findviewbyid(radiobuttonid); int idx = g.indexofchild(radiobutton); if (opsys.getstring(idx + 1).equals(opsys.getstring(5))) { nilai = nilai + 1; } quiz(); } }); quiz(); } public void finish() { next.settext("finish"); next.setbackgroundcolor(color.red); next.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { int radiobuttonid = g.getcheckedradiobuttonid(); view radiobutton = g.findviewbyid(radiobuttonid); int idx = g.indexofchild(radiobutton); if (opsys.getstring(idx + 1).equals(opsys.getstring(5))) { score = score + 1; } exercisequestion.this.finish(); intent showscore = new intent(exercisequestion.this, exercisescore.class); string achievescore = string.valueof(score); bundle bundle = new bundle(); bundle.putstring("achievescore", achievescore); tampilnilai.putextras(bundle); startactivity(showscore); } }); } private void quiz() { question_numb = question_numb + 1; if (question_numb < 10) { qnum.settext(" " + integer.tostring(question_numb) + '.'); } else qnum.settext(integer.tostring(question_numb) + '.'); checkresult(); qcontent.settext(opsys.getstring(0)); //this set question i've taken db textview a.settext(opsys.getstring(1)); //this set option i've taken db radiobutton b.settext(opsys.getstring(2)); //this set option b i've taken db radiobutton c.settext(opsys.getstring(3)); //this set option c i've taken db radiobutton d.settext(opsys.getstring(4)); //this set option d i've taken db radiobutton //to handle when question number 10 if (question_numb == 10) { finish(); } } private boolean checkresult() { // todo auto-generated method stub int random10 = generaterandomnumber(10); //i've method generate random number using math.random() without repetition int random18 = generaterandomnumber(18); string query; bundle bundle = getintent().getextras(); int choosecontent = bundle.getint("choosecontent"); if (choosecontent == 0) { query = "select question, a, b, c, d, answer tbl_exercise (id_topic=" + random18 + " , id_question=" + random10 + ")"; } else { query = "select question, a, b, c, d, answer tbl_exercise (id_topic=" + choosecontent + " , id_question=" + random10 + ")"; } opsys = db.rawquery(query, null); if (opsys != null && opsys.movetofirst()) { return true; } opsys.close(); return false; }
sometimes when touch next button, app crashes (but doesn't, , works charm). logcat follows:
06-04 19:14:16.884 10062-10062/com.coder.learningmodule e/androidruntime﹕ fatal exception: main process: com.coder.learningmodule, pid: 10062 android.database.cursorindexoutofboundsexception: index 0 requested, size of 0 @ android.database.abstractcursor.checkposition(abstractcursor.java:426) @ android.database.abstractwindowedcursor.checkposition(abstractwindowedcursor.java:136) @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:50) @ com.coder.learningmodule.exercisequestion.quiz(exercisequestion.java:106) @ com.coder.learningmodule.exercisequestion.access$300(exercisequestion.java:19) @ com.coder.learningmodule.exercisequestion$1.onclick(exercisequestion.java:68) @ android.view.view.performclick(view.java:4443) @ android.view.view$performclick.run(view.java:18442) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5021) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:827) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:643) @ dalvik.system.nativestart.main(native method)
the error refers qcontent.settext(opsys.getstring(0));
in quiz()
method
after read related question in stackoverflow, have tried following code, not solve problem
opsys = db.rawquery(query, null); if (opsys != null && opsys.movetofirst()) { return true; } opsys.close(); return false;
i appreciate and/or suggestion solve problem. thank much.
you on right path checking opsys
null
, return value of movetofirst()
. method checkresult()
returns true
if these checks successful, or false
otherwise.
your issue don't check return value before working query result:
checkresult(); qcontent.settext(opsys.getstring(0)); //this set question i've taken db textview a.settext(opsys.getstring(1)); //this set option i've taken db radiobutton b.settext(opsys.getstring(2)); //this set option b i've taken db radiobutton c.settext(opsys.getstring(3)); //this set option c i've taken db radiobutton d.settext(opsys.getstring(4)); //this set option d i've taken db radiobutton
instead, check against returned value first before accessing values:
if(checkresult()) { qcontent.settext(opsys.getstring(0)); //this set question i've taken db textview a.settext(opsys.getstring(1)); //this set option i've taken db radiobutton b.settext(opsys.getstring(2)); //this set option b i've taken db radiobutton c.settext(opsys.getstring(3)); //this set option c i've taken db radiobutton d.settext(opsys.getstring(4)); //this set option d i've taken db radiobutton }
this way, accessing query result if there return values. exception caused trying access result has no lines.
Comments
Post a Comment