Android Array List uncaught exception -
i'm trying pull in list of locations db array list can later use create dialog box when call below method can errors below. can see i'm doing wrong , why uncaught exception when catching it? appreciate help!
public void alllocations() { try{ cursor mcursor = mdb.rawquery("select * " + table_location, null); arraylist<string> marraylist = new arraylist<string>(); mcursor.movetofirst(); while(!mcursor.isafterlast()) { marraylist.add(mcursor.getstring(mcursor.getcolumnindex(location))); mcursor.movetonext(); } }catch (sqliteexception e){ log.e("all locations", "error getting locations: " + e.tostring()); } } // errors
06-05 03:36:29.345: d/dalvikvm(2110): gc_external_alloc freed 655 objects / 51272 bytes in 77ms 06-05 03:36:30.185: v/one(2110): locationselection 06-05 03:36:31.805: d/androidruntime(2110): shutting down vm 06-05 03:36:31.805: w/dalvikvm(2110): threadid=1: thread exiting uncaught exception (group=0x4001d800) 06-05 03:36:31.825: e/androidruntime(2110): fatal exception: main 06-05 03:36:31.825: e/androidruntime(2110): java.lang.nullpointerexception 06-05 03:36:31.825: e/androidruntime(2110): @ com.myapp.sqlite.location.alllocations(location.java:137) 06-05 03:36:31.825: e/androidruntime(2110): @ com.myapp.options.displaylocations(options.java:178) 06-05 03:36:31.825: e/androidruntime(2110): @ com.myapp.options$1.onclick(options.java:69) 06-05 03:36:31.825: e/androidruntime(2110): @ android.view.view.performclick(view.java:2408) 06-05 03:36:31.825: e/androidruntime(2110): @ android.view.view$performclick.run(view.java:8816) 06-05 03:36:31.825: e/androidruntime(2110): @ android.os.handler.handlecallback(handler.java:587) 06-05 03:36:31.825: e/androidruntime(2110): @ android.os.handler.dispatchmessage(handler.java:92) 06-05 03:36:31.825: e/androidruntime(2110): @ android.os.looper.loop(looper.java:123) 06-05 03:36:31.825: e/androidruntime(2110): @ android.app.activitythread.main(activitythread.java:4627) 06-05 03:36:31.825: e/androidruntime(2110): @ java.lang.reflect.method.invokenative(native method) 06-05 03:36:31.825: e/androidruntime(2110): @ java.lang.reflect.method.invoke(method.java:521) 06-05 03:36:31.825: e/androidruntime(2110): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 06-05 03:36:31.825: e/androidruntime(2110): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 06-05 03:36:31.825: e/androidruntime(2110): @ dalvik.system.nativestart.main(native method) //the whole class
public class location { private databasehelper mdbhelper; private sqlitedatabase mdb; private final context mctx; private static final string location = ("create table " + table_location + " (" + location + " text," + location_id + " text " + ");"); private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql(location); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + database_name); oncreate(db); } } public location(context ctx) { this.mctx = ctx; } public location open() throws sqlexception { this.mdbhelper = new databasehelper(this.mctx); this.mdb = this.mdbhelper.getwritabledatabase(); return this; } public void close() { this.mdbhelper.close(); } public long addlocation(string locations, string location_id) { contentvalues values = new contentvalues(); values.put(location, locations); values.put(location_id, location_id); return this.mdb.insert(table_location, null, values); } public string getlocation(long l) throws sqlexception { string[] columns = new string[] { location, location_id }; cursor c = mdb.query(table_location, columns, null, null, null, null, null); string result = ""; int glocation = c.getcolumnindex(location); int glocationid = c.getcolumnindex(location_id); (c.movetofirst(); !c.isafterlast(); c.movetonext()) { result = result + c.getstring(glocation) + " " + c.getstring(glocationid) + " " + "\n"; } return result; } /** delete location table */ public void deletealllocations() { mdb.delete(table_location, null, null); } /** check there locations stored */ public boolean countlocation() { cursor count = mdb.rawquery("select count(*) " + table_location, null); if (count == null) { return true; } return false; } public void alllocations() { try{ cursor mcursor = mdb.rawquery("select * " + table_location, null); arraylist<string> marraylist = new arraylist<string>(); mcursor.movetofirst(); while(!mcursor.isafterlast()) { marraylist.add(mcursor.getstring(mcursor.getcolumnindex(location))); mcursor.movetonext(); } }catch (sqliteexception e){ log.e("all locations", "error getting locations: " + e.tostring()); } } }
mdb null @ least in code shown here.
do have other code initializing mdb reference?
edit :]
the change needs made in activity class using class.
you need make sure in activity calling location.open(), before other methods.
Comments
Post a Comment