android - Couldn't read row 0, col -1 from CursorWindow, Cursor initialization error -
i getting error while executing code.
couldn't read row 0, col -1 cursorwindow. make sure cursor initialized correctly before accessing data it.
my db code follows
package com.routecounselor; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class project_db extends sqliteopenhelper { public project_db(context context) { super(context, "mydb.db", null, 1); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub db.execsql("create table route(routeid integer primary key autoincrement,routenumber varchar(5))"); db.execsql("create table stop(stopid integer primary key autoincrement,stopname varchar(40),lat double,lon double)"); db.execsql("create table relation(relationid integer primary key autoincrement,routeid integer, stopid integer," + "foreign key(routeid) references route(routeid), foreign key(stopid) references stop(stopid))"); log.d("logcat", "route table created"); log.d("logcat", "stop table created"); log.d("logcat", "relation table created"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub } public void insertroute() { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put("routenumber", "101"); db.insert("route", null, values); values.put("routenumber", "104"); db.insert("route", null, values); values.put("routenumber", "104-a"); db.insert("route", null, values); values.put("routenumber", "105"); db.insert("route", null, values); values.put("routenumber", "105-a"); db.insert("route", null, values); values.put("routenumber", "110"); db.insert("route", null, values); values.put("routenumber", "111"); db.insert("route", null, values); values.put("routenumber", "113-a"); db.insert("route", null, values); values.put("routenumber", "115"); db.insert("route", null, values); values.put("routenumber", "120"); db.insert("route", null, values); values.put("routenumber", "121"); db.insert("route", null, values); values.put("routenumber", "122"); db.insert("route", null, values); values.put("routenumber", "122-a"); db.insert("route", null, values); values.put("routenumber", "124"); db.insert("route", null, values); values.put("routenumber", "124-a"); db.insert("route", null, values); values.put("routenumber", "127"); db.insert("route", null, values); values.put("routenumber", "127-a"); db.insert("route", null, values); values.put("routenumber", "128"); db.insert("route", null, values); values.put("routenumber", "131"); db.insert("route", null, values); values.put("routenumber", "133"); db.insert("route", null, values); values.put("routenumber", "136"); db.insert("route", null, values); values.put("routenumber", "138"); db.insert("route", null, values); values.put("routenumber", "1"); db.insert("route", null, values); values.put("routenumber", "1-c"); db.insert("route", null, values); values.put("routenumber", "3"); db.insert("route", null, values); values.put("routenumber", "6"); db.insert("route", null, values); values.put("routenumber", "7"); db.insert("route", null, values); values.put("routenumber", "21"); db.insert("route", null, values); values.put("routenumber", "24"); db.insert("route", null, values); db.close(); } public void insertstopname() { sqlitedatabase db = this.getwritabledatabase(); contentvalues values1 = new contentvalues(); values1.put("stopname", "mandimor"); values1.put("lat", "33.637358"); values1.put("lon", "73.035063"); db.insert("stop", null, values1); values1.put("stopname", "pindora"); values1.put("lat", "33.652172"); values1.put("lon", "73.064414"); db.insert("stop", null, values1); values1.put("stopname", "faizabad"); values1.put("lat", "33.663157"); values1.put("lon", " 73.085276"); db.insert("stop", null, values1); values1.put("stopname", "zeropoint"); values1.put("lat", "33.678763"); values1.put("lon", "73.076445"); db.insert("stop", null, values1); values1.put("stopname", "aabpara"); values1.put("lat", " 33.707109"); values1.put("lon", " 73.089308"); db.insert("stop", null, values1); values1.put("stopname", "super market"); values1.put("lat", "33.729444"); values1.put("lon", "73.077584"); db.insert("stop", null, values1); values1.put("stopname", "tarnol"); values1.put("lat", " 33.649919"); values1.put("lon", " 72.910876"); db.insert("stop", null, values1); values1.put("stopname", "g-10/1"); values1.put("lat", " 33.670376"); values1.put("lon", " 73.010287"); db.insert("stop", null, values1); values1.put("stopname", "g-10/2"); values1.put("lat", " 33.677019"); values1.put("lon", " 73.005395"); db.insert("stop", null, values1); values1.put("stopname", "g-10/3"); values1.put("lat", " 33.686465"); values1.put("lon", "73.020801"); db.insert("stop", null, values1); values1.put("stopname", "g-9/3"); values1.put("lat", " 33.696266"); values1.put("lon", " 73.033483"); db.insert("stop", null, values1); values1.put("stopname", "pims"); values1.put("lat", " 33.707050"); values1.put("lon", " 73.054187"); db.insert("stop", null, values1); values1.put("stopname", "g-7 markaz"); values1.put("lat", " 33.720329"); values1.put("lon", " 73.058162"); db.insert("stop", null, values1); values1.put("stopname", "lal quarter"); values1.put("lat", " 33.717243"); values1.put("lon", " 73.073263"); db.insert("stop", null, values1); values1.put("stopname", "polyclinic"); values1.put("lat", " 33.720902"); values1.put("lon", " 73.080108"); db.insert("stop", null, values1); values1.put("stopname", "pak secretariat"); values1.put("lat", " 33.737451"); values1.put("lon", " 73.091737"); db.insert("stop", null, values1); values1.put("stopname", "islamabad chok"); values1.put("lat", " 33.632182"); values1.put("lon", " 72.938578"); db.insert("stop", null, values1); values1.put("stopname", "g-11/1"); values1.put("lat", " 33.658188"); values1.put("lon", " 72.995135"); db.insert("stop", null, values1); values1.put("stopname", "g-11/4"); values1.put("lat", " 33.664504"); values1.put("lon", " 73.000978"); db.insert("stop", null, values1); values1.put("stopname", "g-10 markaz"); values1.put("lat", " 33.676794"); values1.put("lon", " 73.014791"); db.insert("stop", null, values1); values1.put("stopname", "g-9 markaz"); values1.put("lat", "33.689885"); values1.put("lon", "73.030500"); db.insert("stop", null, values1); values1.put("stopname", "blue area"); values1.put("lat", " 33.710953"); values1.put("lon", " 73.060684"); db.insert("stop", null, values1); values1.put("stopname", "parliament chok "); values1.put("lat", "33.729316 "); values1.put("lon", "73.093008"); db.insert("stop", null, values1); values1.put("stopname", "g-15"); values1.put("lat", "33.633175"); values1.put("lon", "72.922142"); db.insert("stop", null, values1); values1.put("stopname", "t , t chok"); values1.put("lat", "33.694879"); values1.put("lon", "73.064160"); db.insert("stop", null, values1); values1.put("stopname", "g-7/2"); values1.put("lat", "33.702493"); values1.put("lon", "73.066129"); db.insert("stop", null, values1); values1.put("stopname", "meloody"); values1.put("lat", "33.716464"); values1.put("lon", "73.083493"); db.insert("stop", null, values1); values1.put("stopname", "foreign office"); values1.put("lat", "33.718699"); values1.put("lon", "73.103647"); db.insert("stop", null, values1); values1.put("stopname", "chattar"); values1.put("lat", "33.779976"); values1.put("lon", "73.240520"); db.insert("stop", null, values1); values1.put("stopname", "bara kau"); values1.put("lat", "33.739015"); values1.put("lon", "73.178387"); db.insert("stop", null, values1); values1.put("stopname", "malpur"); values1.put("lat", "33.730146"); values1.put("lon", "73.145852"); db.insert("stop", null, values1); values1.put("stopname", "rawaldam chok"); values1.put("lat", "33.708651"); values1.put("lon", "73.106199"); db.insert("stop", null, values1); db.close(); } public void insertrelation() { sqlitedatabase db = this.getwritabledatabase(); contentvalues values3 = new contentvalues(); values3.put("routeid", "1"); values3.put("stopid", "1"); db.insert("relation", null, values3); values3.put("routeid", "1"); values3.put("stopid", "2"); db.insert("relation", null, values3); values3.put("routeid", "1"); values3.put("stopid", "3"); db.insert("relation", null, values3); values3.put("routeid", "1"); values3.put("stopid", "4"); db.insert("relation", null, values3); values3.put("routeid", "1"); values3.put("stopid", "5"); db.insert("relation", null, values3); values3.put("routeid", "1"); values3.put("stopid", "6"); db.insert("relation", null, values3); values3.put("routeid", "1"); values3.put("stopid", "49"); db.insert("relation", null, values3); values3.put("routeid", "2"); values3.put("stopid", "7"); db.insert("relation", null, values3); values3.put("routeid", "2"); values3.put("stopid", "8"); db.insert("relation", null, values3); values3.put("routeid", "2"); values3.put("stopid", "9"); db.insert("relation", null, values3); values3.put("routeid", "2"); values3.put("stopid", "10"); db.insert("relation", null, values3); values3.put("routeid", "2"); values3.put("stopid", "11"); db.insert("relation", null, values3); values3.put("routeid", "2"); values3.put("stopid", "12"); db.insert("relation", null, values3); values3.put("routeid", "2"); values3.put("stopid", "13"); db.insert("relation", null, values3); values3.put("routeid", "2"); values3.put("stopid", "14"); db.insert("relation", null, values3); values3.put("routeid", "2"); values3.put("stopid", "15"); db.insert("relation", null, values3); values3.put("routeid", "2"); values3.put("stopid", "6"); db.insert("relation", null, values3); values3.put("routeid", "2"); values3.put("stopid", "16"); db.insert("relation", null, values3); values3.put("routeid", "3"); values3.put("stopid", "7"); db.insert("relation", null, values3); values3.put("routeid", "3"); values3.put("stopid", "17"); db.insert("relation", null, values3); values3.put("routeid", "3"); values3.put("stopid", "18"); db.insert("relation", null, values3); values3.put("routeid", "3"); values3.put("stopid", "19"); db.insert("relation", null, values3); values3.put("routeid", "3"); values3.put("stopid", "20"); db.insert("relation", null, values3); values3.put("routeid", "3"); values3.put("stopid", "21"); db.insert("relation", null, values3); values3.put("routeid", "3"); values3.put("stopid", "12"); db.insert("relation", null, values3); values3.put("routeid", "3"); values3.put("stopid", "22"); db.insert("relation", null, values3); values3.put("routeid", "3"); values3.put("stopid", "15"); db.insert("relation", null, values3); values3.put("routeid", "3"); values3.put("stopid", "23"); db.insert("relation", null, values3); values3.put("routeid", "3"); values3.put("stopid", "16"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "24"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "19"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "20"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "9"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "10"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "21"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "25"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "26"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "13"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "14"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "15"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "27"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "5"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "28"); db.insert("relation", null, values3); values3.put("routeid", "4"); values3.put("stopid", "16"); db.insert("relation", null, values3); values3.put("routeid", "5"); values3.put("stopid", "24"); db.insert("relation", null, values3); values3.put("routeid", "5"); values3.put("stopid", "20"); db.insert("relation", null, values3); values3.put("routeid", "5"); values3.put("stopid", "21"); db.insert("relation", null, values3); values3.put("routeid", "5"); values3.put("stopid", "25"); db.insert("relation", null, values3); values3.put("routeid", "5"); values3.put("stopid", "26"); db.insert("relation", null, values3); values3.put("routeid", "5"); values3.put("stopid", "13"); db.insert("relation", null, values3); values3.put("routeid", "5"); values3.put("stopid", "14"); db.insert("relation", null, values3); values3.put("routeid", "5"); values3.put("stopid", "15"); db.insert("relation", null, values3); values3.put("routeid", "5"); values3.put("stopid", "27"); db.insert("relation", null, values3); values3.put("routeid", "5"); values3.put("stopid", "5"); db.insert("relation", null, values3); values3.put("routeid", "5"); values3.put("stopid", "28"); db.insert("relation", null, values3); values3.put("routeid", "5"); values3.put("stopid", "16"); db.insert("relation", null, values3); values3.put("routeid", "6"); values3.put("stopid", "29"); db.insert("relation", null, values3); values3.put("routeid", "6"); values3.put("stopid", "30"); db.insert("relation", null, values3); values3.put("routeid", "6"); values3.put("stopid", "31"); db.insert("relation", null, values3); values3.put("routeid", "6"); values3.put("stopid", "32"); db.insert("relation", null, values3); values3.put("routeid", "6"); values3.put("stopid", "3"); db.insert("relation", null, values3); values3.put("routeid", "6"); values3.put("stopid", "2"); db.insert("relation", null, values3); values3.put("routeid", "6"); values3.put("stopid", "33"); db.insert("relation", null, values3); values3.put("routeid", "6"); values3.put("stopid", "1"); db.insert("relation", null, values3); values3.put("routeid", "7"); values3.put("stopid", "34"); db.insert("relation", null, values3); values3.put("routeid", "7"); values3.put("stopid", "35"); db.insert("relation", null, values3); values3.put("routeid", "7"); values3.put("stopid", "36"); db.insert("relation", null, values3); values3.put("routeid", "7"); values3.put("stopid", "3"); db.insert("relation", null, values3); values3.put("routeid", "7"); values3.put("stopid", "37"); db.insert("relation", null, values3); values3.put("routeid", "7"); values3.put("stopid", "38"); db.insert("relation", null, values3); values3.put("routeid", "7"); values3.put("stopid", "12"); db.insert("relation", null, values3); values3.put("routeid", "7"); values3.put("stopid", "39"); db.insert("relation", null, values3); values3.put("routeid", "8"); values3.put("stopid", "1"); db.insert("relation", null, values3); values3.put("routeid", "8"); values3.put("stopid", "40"); db.insert("relation", null, values3); values3.put("routeid", "8"); values3.put("stopid", "41"); db.insert("relation", null, values3); values3.put("routeid", "8"); values3.put("stopid", "21"); db.insert("relation", null, values3); values3.put("routeid", "8"); values3.put("stopid", "42"); db.insert("relation", null, values3); values3.put("routeid", "8"); values3.put("stopid", "12"); db.insert("relation", null, values3); values3.put("routeid", "8"); values3.put("stopid", "22"); db.insert("relation", null, values3); values3.put("routeid", "8"); values3.put("stopid", "6"); db.insert("relation", null, values3); values3.put("routeid", "8"); values3.put("stopid", "16"); db.insert("relation", null, values3); values3.put("routeid", "9"); values3.put("stopid", "1"); db.insert("relation", null, values3); values3.put("routeid", "9"); values3.put("stopid", "48"); db.insert("relation", null, values3); values3.put("routeid", "9"); values3.put("stopid", "40"); db.insert("relation", null, values3); values3.put("routeid", "9"); values3.put("stopid", "41"); db.insert("relation", null, values3); values3.put("routeid", "9"); values3.put("stopid", "21"); db.insert("relation", null, values3); values3.put("routeid", "9"); values3.put("stopid", "20"); db.insert("relation", null, values3); values3.put("routeid", "9"); values3.put("stopid", "43"); db.insert("relation", null, values3); values3.put("routeid", "9"); values3.put("stopid", "44"); db.insert("relation", null, values3); values3.put("routeid", "9"); values3.put("stopid", "18"); db.insert("relation", null, values3); db.close(); } public string matchstop(string data1,string data2) { string ab=null; string source=null; string destination=null; string latitude1=null; string longitude1=null; string latitude2=null; string longitude2=null; sqlitedatabase db=this.getreadabledatabase(); string select1query = "select * stop stopname='"+data1+"'"; cursor cursor=db.rawquery(select1query,null); if(cursor.movetofirst()){ do{ source=cursor.getstring(cursor.getcolumnindex("stopname")); latitude1=cursor.getstring(cursor.getcolumnindex("lat")); longitude1=cursor.getstring(cursor.getcolumnindex("lon")); /*log.e("lat lan",latitude+longitude+""); log.e("n",source+"");*/ } while(cursor.movetonext()); } string select2query = "select * stop stopname='"+data2+"'"; cursor cursor1=db.rawquery(select2query,null); if(cursor1.movetofirst()){ do{ destination=cursor1.getstring(cursor1.getcolumnindex("stopname")); latitude2=cursor1.getstring(cursor1.getcolumnindex("lat")); longitude2=cursor1.getstring(cursor1.getcolumnindex("lon")); } while(cursor.movetonext()); if(source!=null && destination!=null) { ab= (source+",,"+destination+",,"+latitude1+",,"+longitude1+",,"+latitude2+",,"+longitude2); } else { } if(latitude1!=null && longitude1!=null){ } } return ab; } public string getroutenumber(string source,string destination) { system.out.println("inside getroutenumber"); string number=null; string number1 = null; string number2 = null; sqlitedatabase db = this.getreadabledatabase(); string selectquery1="select * stop stopname='"+source+"'"; cursor cursor1 = db.rawquery(selectquery1, null); if (cursor1.movetofirst()) { { number1 = cursor1.getstring(cursor1.getcolumnindex("stopid")); log.e("source id", number1 ); } while (cursor1.movetonext()); } string selectquery2="select * stop stopname='"+destination+"'"; cursor cursor2 = db.rawquery(selectquery2, null); if (cursor2.movetofirst()) { { number2 = cursor2.getstring(cursor2.getcolumnindex("stopid")); log.e("destination id", number2); } while (cursor1.movetonext()); } string selectquery = "select stopid relation routeid in(select routeid relation stopid='"+number1+"')and stopid in(select stopid relation routeid in(select routeid relation stopid='"+number2+"'))"; cursor cursor = db.rawquery(selectquery, null); system.out.println(cursor); if (cursor.movetofirst()) { { log.e("inside", "of do"); number = cursor.getstring(cursor.getcolumnindex("routenumber")); log.e("route number", number ); } while (cursor.movetonext()); } return number; } }
my cat log
08-18 00:06:31.957: e/androidruntime(22127): fatal exception: main 08-18 00:06:31.957: e/androidruntime(22127): java.lang.illegalstateexception: couldn't read row 0, col -1 cursorwindow. make sure cursor initialized correctly before accessing data it. 08-18 00:06:31.957: e/androidruntime(22127): @ android.database.cursorwindow.nativegetstring(native method) 08-18 00:06:31.957: e/androidruntime(22127): @ android.database.cursorwindow.getstring(cursorwindow.java:434) 08-18 00:06:31.957: e/androidruntime(22127): @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:51) 08-18 00:06:31.957: e/androidruntime(22127): @ com.routecounselor.project_db.getroutenumber(project_db.java:2141) 08-18 00:06:31.957: e/androidruntime(22127): @ com.routecounselor.map$1.onclick(map.java:136) 08-18 00:06:31.957: e/androidruntime(22127): @ android.view.view.performclick(view.java:4421) 08-18 00:06:31.957: e/androidruntime(22127): @ android.view.view$performclick.run(view.java:17903) 08-18 00:06:31.957: e/androidruntime(22127): @ android.os.handler.handlecallback(handler.java:730) 08-18 00:06:31.957: e/androidruntime(22127): @ android.os.handler.dispatchmessage(handler.java:92) 08-18 00:06:31.957: e/androidruntime(22127): @ android.os.looper.loop(looper.java:213) 08-18 00:06:31.957: e/androidruntime(22127): @ android.app.activitythread.main(activitythread.java:5225) 08-18 00:06:31.957: e/androidruntime(22127): @ java.lang.reflect.method.invokenative(native method) 08-18 00:06:31.957: e/androidruntime(22127): @ java.lang.reflect.method.invoke(method.java:525) 08-18 00:06:31.957: e/androidruntime(22127): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:741) 08-18 00:06:31.957: e/androidruntime(22127): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:557) 08-18 00:06:31.957: e/androidruntime(22127): @ dalvik.system.nativestart.main(native method)
can please detect error is?
the error shown in logcat says when called getcolumnindex()
within getroutenumber()
returned -1, indicating column name not exist in retrieved data set. make sure sql query correct , column name matches column in table exactly. if can't figure out problem, try incrementally replacing (just sake of debugging) getcolumnindex() methods explicit column index , see error pops.
Comments
Post a Comment