Tuesday 15 January 2013

android - IllegalStateException: couldn't read row 0, col -1 from cursorWindow -



android - IllegalStateException: couldn't read row 0, col -1 from cursorWindow -

illegalstateexception: couldn't read row 0, col -1 cursorwindow. new android looked @ smiller question did solve problem help please in advance.

here code:

// function add together info in db public void createxpenses(view view) { mcategory = (spinner )findviewbyid(r.id.spcategory); edittext edittext2 = (edittext )findviewbyid(r.id.amountedittext); edittext edittext3 = (edittext )findviewbyid(r.id.paytypeedittext); edittext edittext4 = (edittext )findviewbyid(r.id.dateedittext); category =mcategory.getselecteditem().tostring(); amount =edittext2.gettext().tostring(); paymenttype =edittext3.gettext().tostring(); date =edittext4.gettext().tostring(); db.execsql("insert expenses values('"+category+"','"+amount+"','"+paymenttype+"','"+date+"');"); toast.maketext(getbasecontext(), "your info saved successfully!", toast.length_short).show(); } public void showexpens(view view) { cursor c=db.rawquery("select * expenses", null); int count= c.getcount(); c.movetofirst(); tablelayout tablelayout = new tablelayout(getapplicationcontext()); tablelayout.setverticalscrollbarenabled(true); tablerow tablerow; textview textview,textview1,textview2,textview3,textview4,textview5,textview6; tablerow = new tablerow(getapplicationcontext()); textview=new textview(getapplicationcontext()); textview.settext("expense_category"); textview.settextcolor(color.red); textview.settypeface(null, typeface.bold); textview.setpadding(20, 20, 20, 20); tablerow.addview(textview); textview4=new textview(getapplicationcontext()); textview4.settext("amount"); textview4.settextcolor(color.green); textview4.settypeface(null, typeface.bold); textview4.setpadding(20, 20, 20, 20); tablerow.addview(textview4); textview5=new textview(getapplicationcontext()); textview5.settext("paymenttype"); textview5.settextcolor(color.red); textview5.settypeface(null, typeface.bold); textview5.setpadding(20, 20, 20, 20); tablerow.addview(textview5); textview6 = new textview(getapplicationcontext()); textview6.settext("date"); textview6.settextcolor(color.red); textview6.settypeface(null, typeface.bold); textview6.setpadding(20, 20, 20, 20); tablerow.addview(textview6); tablelayout.addview(tablerow); (integer j = 0; j < count; j++) { tablerow = new tablerow(getapplicationcontext()); textview1 = new textview(getapplicationcontext()); textview1.settext(c.getstring(c.getcolumnindex("expense_category "))); textview2 = new textview(getapplicationcontext()); textview2.settext(c.getstring(c.getcolumnindex("amount"))); textview3 = new textview(getapplicationcontext()); textview3.settext(c.getstring(c.getcolumnindex("paymenttype"))); textview4 = new textview(getapplicationcontext()); textview4.settext(c.getstring(c.getcolumnindex("date"))); textview1.setpadding(20, 20, 20, 20); textview2.setpadding(20, 20, 20, 20); textview3.setpadding(20, 20, 20, 20); textview4.setpadding(20, 20, 20, 20); tablerow.addview(textview1); tablerow.addview(textview2); tablerow.addview(textview3); tablerow.addview(textview4); tablelayout.addview(tablerow); c.movetonext() ; } setcontentview(tablelayout); db.close(); } public void close(view view) { system.exit(0); } }

this logcat

11-12 09:50:01.978: e/cursorwindow(871): failed read row 0, column -1 cursorwindow has 1 rows, 4 columns. 11-12 09:50:01.988: d/androidruntime(871): shutting downwards vm 11-12 09:50:01.988: w/dalvikvm(871): threadid=1: thread exiting uncaught exception (group=0x41465700) 11-12 09:50:02.148: e/androidruntime(871): fatal exception: main 11-12 09:50:02.148: e/androidruntime(871): java.lang.illegalstateexception: not execute method of activity 11-12 09:50:02.148: e/androidruntime(871): @ android.view.view$1.onclick(view.java:3633) 11-12 09:50:02.148: e/androidruntime(871): @ android.view.view.performclick(view.java:4240) 11-12 09:50:02.148: e/androidruntime(871): @ android.view.view$performclick.run(view.java:17721) 11-12 09:50:02.148: e/androidruntime(871): @ android.os.handler.handlecallback(handler.java:730) 11-12 09:50:02.148: e/androidruntime(871): @ android.os.handler.dispatchmessage(handler.java:92) 11-12 09:50:02.148: e/androidruntime(871): @ android.os.looper.loop(looper.java:137) 11-12 09:50:02.148: e/androidruntime(871): @ android.app.activitythread.main(activitythread.java:5103) 11-12 09:50:02.148: e/androidruntime(871): @ java.lang.reflect.method.invokenative(native method) 11-12 09:50:02.148: e/androidruntime(871): @ java.lang.reflect.method.invoke(method.java:525) 11-12 09:50:02.148: e/androidruntime(871): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 11-12 09:50:02.148: e/androidruntime(871): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 11-12 09:50:02.148: e/androidruntime(871): @ dalvik.system.nativestart.main(native method) 11-12 09:50:02.148: e/androidruntime(871): caused by: java.lang.reflect.invocationtargetexception 11-12 09:50:02.148: e/androidruntime(871): @ java.lang.reflect.method.invokenative(native method) 11-12 09:50:02.148: e/androidruntime(871): @ java.lang.reflect.method.invoke(method.java:525) 11-12 09:50:02.148: e/androidruntime(871): @ android.view.view$1.onclick(view.java:3628) 11-12 09:50:02.148: e/androidruntime(871): ... 11 more 11-12 09:50:02.148: e/androidruntime(871): caused by: java.lang.illegalstateexception: couldn't read row 0, col -1 cursorwindow. create sure cursor initialized correctly before accessing info it. 11-12 09:50:02.148: e/androidruntime(871): @ android.database.cursorwindow.nativegetstring(native method) 11-12 09:50:02.148: e/androidruntime(871): @ android.database.cursorwindow.getstring(cursorwindow.java:434) 11-12 09:50:02.148: e/androidruntime(871): @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:51) 11-12 09:50:02.148: e/androidruntime(871): @ com.info.househouldexpcalulator.mainactivity.showexpens(mainactivity.java:113) 11-12 09:50:02.148: e/androidruntime(871): ... 14 more 11-12 09:50:02.308: d/dalvikvm(871): gc_for_alloc freed 178k, 8% free 2841k/3076k, paused 54ms, total 89ms

the column name gave not exists.

getcolumnindex(string columnname)

returns zero-based index given column name, or -1 if column doesn't exist. error lies in of next parts of code:

c.getstring(c.getcolumnindex("expense_category ")); //notice space after expense_category c.getstring(c.getcolumnindex("amount")); c.getstring(c.getcolumnindex("paymenttype")); c.getstring(c.getcolumnindex("date"));

if expect column exist utilize getcolumnindexorthrow(string) instead, create error more clear.

android sqlite cursor

No comments:

Post a Comment