java - Kotlin Database Error CursorIndexOutOfBoundsException -
this error, says index out of bounds, couldn't how can solve it, there turkish words aren't important, think:
e/androidruntime: fatal exception: main process: com.example.burhanozen.nothesaplama, pid: 26919 java.lang.runtimeexception: unable start activity componentinfo{com.example.burhanozen.nothesaplama/com.example.burhanozen.nothesaplama.mainactivity}: android.database.cursorindexoutofboundsexception: index 1 requested, size of 1 @ android.app.activitythread.performlaunchactivity(activitythread.java:2665) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2726) @ ........
i trying store kind of student information. have these errors, , share codes below. mainactivity:
override fun oncreate(savedinstancestate: bundle?) { super.oncreate(savedinstancestate) setcontentview(r.layout.activity_main) val toolbar = findviewbyid(r.id.toolbar) toolbar setsupportactionbar(toolbar) val fab = findviewbyid(r.id.fab) floatingactionbutton fab.setonclicklistener { val intent = intent(this@mainactivity,notekrani::class.java) startactivity(intent) } val studentsarray = arraylist<string>() val arrayadapter = arrayadapter(this,android.r.layout.simple_list_item_1,studentsarray) listview.adapter= arrayadapter try{ val mydatabase = this.openorcreatedatabase("student", context.mode_private,null) mydatabase.execsql("create table if not exists students (isim text," + " dersbir text, dersbirkredi text, dersbirort text," + " dersiki text, dersikikredi text, dersikiort text) ") val cursor = mydatabase.rawquery("select * students",null) val nameix = cursor.getcolumnindex("isim") val dersbirix = cursor.getcolumnindex("dersbir") val dersbirkrediix = cursor.getcolumnindex("dersbirkredi") val dersbirortix = cursor.getcolumnindex("dersbirort") val dersikiix = cursor.getcolumnindex("dersiki") val dersikikrediix = cursor.getcolumnindex("dersikikredi") val dersikiortix = cursor.getcolumnindex("dersikiort") cursor.movetonext() while (cursor != null){ studentsarray.add(cursor.getstring(nameix)) studentsarray.add(cursor.getstring(dersbirix)) studentsarray.add(cursor.getstring(dersbirkrediix)) studentsarray.add(cursor.getstring(dersbirortix)) studentsarray.add(cursor.getstring(dersikiix)) studentsarray.add(cursor.getstring(dersikikrediix)) studentsarray.add(cursor.getstring(dersikiortix)) cursor.movetonext() arrayadapter.notifydatasetchanged() } while (cursor!=null){ cursor!!.close() } }catch (e:sqlexception){ }
and second activity page, couldn't error here, may can help. wrote database codes here.
fun kaydet(view:view){ val isim = isim.text.tostring() val dersbir = dersbir.text.tostring() val dersbirkredi = dersbirkredi.text.tostring() val dersbirort = dersbirort.text.tostring() val dersiki = dersiki.text.tostring() val dersikikredi = dersikikredi.text.tostring() val dersikiort = dersikiort.text.tostring() try { val mydatabase = openorcreatedatabase("student", context.mode_private,null) mydatabase.execsql("create table if not exists students (isim text," + " dersbir text, dersbirkredi text, dersbirort text," + " dersiki text, dersikikredi text, dersikiort text) ") val sqlstring = "insert students (isim,dersbir,dersbirkredi,dersbirort,dersiki,dersikikredi,dersikiort) values(?,?,?,?,?,?,?)" val statement = mydatabase.compilestatement(sqlstring) statement.bindstring(1,isim) statement.bindstring(2,dersbir) statement.bindstring(3,dersbirkredi) statement.bindstring(4,dersbirort) statement.bindstring(5,dersiki) statement.bindstring(6,dersikikredi) statement.bindstring(7,dersikiort) statement.execute() }catch (e:sqlexception){ } val intent = intent(this,mainactivity::class.java) startactivity(intent) }
change while loop this:
while (cursor.movetonext()){ studentsarray.add(cursor.getstring(nameix)) ... }
(remove other cursor.movetonext()
code)
you checking cursor != null
in while loop condition, won't become null after read rows cursor.
in above code, cursor.movetonext
returns true if there row next. otherwise returns false , loop terminates.
and, noticed, should change from,
while (cursor!=null){ cursor!!.close() }
to if check:
if (cursor!=null){ cursor.close() }
or, todd suggested in comments, can use higher order function use
automatically closes cursor, simlar java 7 try-with-resources.
cursor.use { while (cursor.movetonext()){ studentsarray.add(cursor.getstring(nameix)) ... } }
Comments
Post a Comment