android - java - java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow -
i'm creating app uses sqlitedatabase store items of listview
. but, error while populating data in listview
database, , app crashes.
here's database -
import java.util.arraylist; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class notesdatabase extends sqliteopenhelper { private static final string note_name = "name"; private static final string note_id = "id"; private static final string note_subject = "subject"; private static final string database_name = "notesdatabase"; private static final string table_notes = "notes"; private static final int database_version = 1; private static final string create_notes_table = "create table " + table_notes + "(" + note_id + " integer primary key," + note_name + " text," + note_subject + " text" + ")"; public notesdatabase(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { try { db.execsql(create_notes_table); } catch(sqlexception e) { e.printstacktrace(); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exsists topics"); oncreate(db); } public void addnote(note note) { sqlitedatabase db = this.getwritabledatabase(); contentvalues notes = new contentvalues(); notes.put(note_name, note.getnote()); notes.put(note_subject, note.getsubject()); db.insert(table_notes, null, notes); db.close(); } public void removenote(note note) { sqlitedatabase db = this.getwritabledatabase(); db.delete(table_notes, note_name + " = ?", new string[] {note.getnote()}); db.close(); } public arraylist<note> getnotes(){ arraylist<note> notes = new arraylist<note>(); string selectquery = "select * " + table_notes; sqlitedatabase db = this.getwritabledatabase(); cursor cur = db.rawquery(selectquery, null); if(cur.movetofirst()) { { note note = new note(); note.setid(integer.parseint(cur.getstring(0))); note.setnote(cur.getstring(cur.getcolumnindex(note_name))); note.setsubject(cur.getstring(cur.getcolumnindex(note_subject))); notes.add(note); } while(cur.movetonext()); } db.close(); return notes; } public void updatenote(string old_name, string new_name) { sqlitedatabase db = this.getwritabledatabase(); contentvalues topic = new contentvalues(); topic.put(note_name, new_name); db.update(table_notes, topic, note_name + " = ?", new string[] {old_name}); db.close(); } }
note.java -
public class note { string _note; int _id; string subject; public string getsubject() { return subject; } public void setsubject(string subject) { this.subject = subject; } public note() { } public note(int id, string note, string subject) { this._id = id; this._note = note; this.subject = subject; } public note(string note, string subject) { this._note = note; this.subject = subject; } public note(string note) { this._note = note; } public string getnote() { return _note; } public void setnote(string _note) { this._note = _note; } public int getid() { return _id; } public void setid(int _id) { this._id = _id; } }
notesdatabase.java -
import java.util.arraylist; import android.os.bundle; import android.app.activity; import android.content.intent; import android.util.log; import android.view.menu; import android.view.view; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class notes_page extends activity { arraylist<note> notes = new arraylist<note>(); notesdatabase ndb = new notesdatabase(this); private noteadapter adapter; private listview notes_list; private textview subject_display; string subject; int requestcode = 1; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_notes_page); notes_list = (listview) findviewbyid(r.id.notes); ndb.getwritabledatabase(); notes = ndb.getnotes(); adapter = new noteadapter(this, notes); subject = getintent().getstringextra("subject_name"); notes_list.setadapter(adapter); subject_display = (textview) findviewbyid(r.id.subject_name); subject_display.settext(subject); } public void onclickaddnewnote(view v) { startactivityforresult(new intent("com.swap.add_new_note"), requestcode); } public void onactivityresult(int request_code, int result_code, intent i) { if(request_code == requestcode) { if(result_code == result_ok) { note mnote = new note(i.getstringextra("note"), getintent().getstringextra("subject_name")); ndb.getwritabledatabase(); ndb.addnote(mnote); log.d("addnote", "note succesfully added database"); adapter.add(mnote); adapter.notifydatasetchanged(); toast.maketext(getbasecontext(), mnote.getnote(), toast.length_short).show(); } } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_notes_page, menu); return true; } }
here's logcat error log -
04-13 22:17:10.766: e/linker(25692): load_library(linker.cpp:759): library "libmaliinstr.so" not found 04-13 22:17:14.816: e/cursorwindow(25692): failed read row 0, column -1 cursorwindow has 1 rows, 2 columns. 04-13 22:17:14.869: e/androidruntime(25692): fatal exception: main 04-13 22:17:14.869: e/androidruntime(25692): java.lang.runtimeexception: unable start activity componentinfo{com.swap.rr/com.swap.rr.notes_page}: java.lang.illegalstateexception: couldn't read row 0, col -1 cursorwindow. make sure cursor initialized correctly before accessing data it. 04-13 22:17:14.869: e/androidruntime(25692): @ android.app.activitythread.performlaunchactivity(activitythread.java:2364) 04-13 22:17:14.869: e/androidruntime(25692): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2416) 04-13 22:17:14.869: e/androidruntime(25692): @ android.app.activitythread.access$600(activitythread.java:174) 04-13 22:17:14.869: e/androidruntime(25692): @ android.app.activitythread$h.handlemessage(activitythread.java:1382) 04-13 22:17:14.869: e/androidruntime(25692): @ android.os.handler.dispatchmessage(handler.java:107) 04-13 22:17:14.869: e/androidruntime(25692): @ android.os.looper.loop(looper.java:194) 04-13 22:17:14.869: e/androidruntime(25692): @ android.app.activitythread.main(activitythread.java:5409) 04-13 22:17:14.869: e/androidruntime(25692): @ java.lang.reflect.method.invokenative(native method) 04-13 22:17:14.869: e/androidruntime(25692): @ java.lang.reflect.method.invoke(method.java:525) 04-13 22:17:14.869: e/androidruntime(25692): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 04-13 22:17:14.869: e/androidruntime(25692): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:606) 04-13 22:17:14.869: e/androidruntime(25692): @ dalvik.system.nativestart.main(native method) 04-13 22:17:14.869: e/androidruntime(25692): caused by: java.lang.illegalstateexception: couldn't read row 0, col -1 cursorwindow. make sure cursor initialized correctly before accessing data it. 04-13 22:17:14.869: e/androidruntime(25692): @ android.database.cursorwindow.nativegetstring(native method) 04-13 22:17:14.869: e/androidruntime(25692): @ android.database.cursorwindow.getstring(cursorwindow.java:434) 04-13 22:17:14.869: e/androidruntime(25692): @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:51) 04-13 22:17:14.869: e/androidruntime(25692): @ com.swap.rr.notesdatabase.getnotes(notesdatabase.java:78) 04-13 22:17:14.869: e/androidruntime(25692): @ com.swap.rr.notes_page.oncreate(notes_page.java:41) 04-13 22:17:14.869: e/androidruntime(25692): @ android.app.activity.performcreate(activity.java:5122) 04-13 22:17:14.869: e/androidruntime(25692): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1146) 04-13 22:17:14.869: e/androidruntime(25692): @ android.app.activitythread.performlaunchactivity(activitythread.java:2328) 04-13 22:17:14.869: e/androidruntime(25692): ... 11 more
please me figure out what's wrong me code. in advance!!
your subject line misleading. exception in stacktrace says
couldn't read row 0, col -1 cursorwindow
which means getcolumnindex()
not find specified column in cursor
, -1 returned column index.
your selection select *
contains columns table.
your create table
seems contain columns you're requesting index of getcolumnindex()
in getnotes()
.
chances you've added column test device still has database file without column. uninstall app or clear data remove old database file , make oncreate()
recreate database on next run.
Comments
Post a Comment