android - Getting Error java.lang.IllegalStateException: get field slot from row 0 col -1 failed -
helper class:
package com.deangrobler.myapp; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.*; public class databasehelper extends sqliteopenhelper{ //the android's default system path of application database. private static string db_path = "/data/data/com.deangrobler.chumperoo/databases/"; private static string db_name = "chumperoodb.db"; private sqlitedatabase mydatabase; private final context mycontext; /** * constructor * takes , keeps reference of passed context in order access application assets , resources. * @param context */ public databasehelper(context context) { super(context, db_name, null, 1); this.mycontext = context; } /** * creates empty database on system , rewrites own database. * */ public void createdatabase() throws ioexception{ boolean dbexist = checkdatabase(); if(dbexist){ //do nothing - database exist }else{ //by calling method , empty database created default system path //of application gonna able overwrite database our database. this.getreadabledatabase(); try { copydatabase(); } catch (ioexception e) { throw new error("error copying database: " + e.getstacktrace()); } } } /** * check if database exist avoid re-copying file each time open application. * @return true if exists, false if doesn't */ private boolean checkdatabase(){ sqlitedatabase checkdb = null; try{ string mypath = db_path + db_name; checkdb = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); }catch(sqliteexception e){ //database does't exist yet. } if(checkdb != null){ checkdb.close(); } return checkdb != null ? true : false; } /** * copies database local assets-folder created empty database in * system folder, can accessed , handled. * done transfering bytestream. * */ private void copydatabase() throws ioexception{ //open local db input stream inputstream myinput = mycontext.getassets().open(db_name); // path created empty db string outfilename = db_path + db_name; //open empty db output stream outputstream myoutput = new fileoutputstream(outfilename); //transfer bytes inputfile outputfile byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer))>0){ myoutput.write(buffer, 0, length); } //close streams myoutput.flush(); myoutput.close(); myinput.close(); } public void opendatabase() throws sqlexception{ //open database string mypath = db_path + db_name; mydatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readwrite); } @override public synchronized void close() { if(mydatabase != null) mydatabase.close(); super.close(); } @override public void oncreate(sqlitedatabase db) { } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } //queries database public cursor querydatabase(string table, string[] columns, string selection){ return mydatabase.query(table, columns, selection, null, null, null, null); } }
using in main.java so:
databasehelper mydbhelper = new databasehelper(this); try { mydbhelper.createdatabase(); } catch (ioexception ioe) { throw new error("unable create database"); } try { mydbhelper.opendatabase(); }catch(sqlexception sqle){ throw sqle; } //get cursor , print out data cursor returnedcur = mydbhelper.querydatabase("expenses", new string[]{"name","amount"}, null); if (returnedcur.movetofirst()){ do{ string data = returnedcur.getstring(returnedcur.getcolumnindex("data")); system.out.println("returned data: "+data); }while(returnedcur.movetonext()); } returnedcur.close();
and finaly entire log:
> 06-05 13:28:35.885: e/cursorwindow(733): bad request field slot 0,-1. numrows = 1, numcolumns = 2 06-05 13:28:35.885: d/androidruntime(733): shutting down vm 06-05 13:28:35.885: w/dalvikvm(733): threadid=1: thread exiting uncaught exception (group=0x40015560) 06-05 13:28:35.904: e/androidruntime(733): fatal exception: main 06-05 13:28:35.904: e/androidruntime(733): **java.lang.runtimeexception: unable start activity componentinfo{com.deangrobler.chumperoo/com.deangrobler.chumperoo.main}: java.lang.illegalstateexception: field slot row 0 col -1 failed** 06-05 13:28:35.904: e/androidruntime(733): @ android.app.activitythread.performlaunchactivity(activitythread.java:1622) 06-05 13:28:35.904: e/androidruntime(733): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1638) 06-05 13:28:35.904: e/androidruntime(733): @ android.app.activitythread.access$1500(activitythread.java:117) 06-05 13:28:35.904: e/androidruntime(733): @ android.app.activitythread$h.handlemessage(activitythread.java:928) 06-05 13:28:35.904: e/androidruntime(733): @ android.os.handler.dispatchmessage(handler.java:99) 06-05 13:28:35.904: e/androidruntime(733): @ android.os.looper.loop(looper.java:123) 06-05 13:28:35.904: e/androidruntime(733): @ android.app.activitythread.main(activitythread.java:3647) 06-05 13:28:35.904: e/androidruntime(733): @ java.lang.reflect.method.invokenative(native method) 06-05 13:28:35.904: e/androidruntime(733): @ java.lang.reflect.method.invoke(method.java:507) 06-05 13:28:35.904: e/androidruntime(733): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 06-05 13:28:35.904: e/androidruntime(733): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 06-05 13:28:35.904: e/androidruntime(733): @ dalvik.system.nativestart.main(native method) 06-05 13:28:35.904: e/androidruntime(733): caused by: java.lang.illegalstateexception: field slot row 0 col -1 failed 06-05 13:28:35.904: e/androidruntime(733): @ android.database.cursorwindow.getstring_native(native method) 06-05 13:28:35.904: e/androidruntime(733): @ android.database.cursorwindow.getstring(cursorwindow.java:329) 06-05 13:28:35.904: e/androidruntime(733): @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:49) 06-05 13:28:35.904: e/androidruntime(733): @ com.deangrobler.chumperoo.main.oncreate(main.java:55) 06-05 13:28:35.904: e/androidruntime(733): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 06-05 13:28:35.904: e/androidruntime(733): @ android.app.activitythread.performlaunchactivity(activitythread.java:1586) 06-05 13:28:35.904: e/androidruntime(733): ... 11 more 06-05 13:28:53.264: i/process(733): sending signal. pid: 733 sig: 9
no idea why happening, i've been googling half day , nothing helping me sort out :-(
new string[]{"name","amount"}
you asking "data" field cant found field name "data" in db. can try
string name = returnedcur.getstring(returnedcur.getcolumnindex("name"));
Comments
Post a Comment