java - Can't find my NullPointerException -
i getting nullpointerexception can't see. know it's simple i'm overlooking. have bit reads in text file line line splits it, , uses data create custom class , pass data off added sqlite database.
public void createdata() { try { inputstream in = this.getassets().open("stops.txt"); bufferedreader reader = new bufferedreader( new inputstreamreader(in)); string line; line=reader.readline(); while ((line = reader.readline())!=null) { string linevalues[] = line.split(","); stops stop = new stops(); stop.setlon(linevalues[5]); stop.setlat(linevalues[4]); stop.setname(linevalues[2]); stop.setnumber(linevalues[0]); datasource.create(stop); } }catch (ioexception e) { e.printstacktrace(); }catch(nullpointerexception n) { n.printstacktrace(); log.d(tag,n.tostring()); } }
the exception occurring on datasource.create(stop);
tips out there?
edit: here stack trace:
java.lang.runtimeexception: unable start activity componentinfo{ccalgary.transit.helper/ccalgary.transit.helper.mainactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2306) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2356) @ android.app.activitythread.access$600(activitythread.java:150) @ android.app.activitythread$h.handlemessage(activitythread.java:1244) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5195) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:795) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:562) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ ccalgary.transit.helper.mainactivity.createdata(mainactivity.java:341) @ ccalgary.transit.helper.mainactivity.oncreate(mainactivity.java:71) @ android.app.activity.performcreate(activity.java:5104) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) @ android.app.activitythread.performlaunchactivity(activitythread.java:2260)
and here oncreate
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); setrequestedorientation(activityinfo.screen_orientation_portrait); settings = preferencemanager.getdefaultsharedpreferences(this); spreferences = preferencemanager.getdefaultsharedpreferences(this); scontext = getapplicationcontext(); boolean boot = settings.getboolean("launch", false); if (boot == false) { createdata(); } datasource = new stopsdatasource(this); dbhelper = new stopsdbhelper(this,table_stops,null,1); mdatabase = dbhelper.getwritabledatabase(); datasource.open(); if (mdatabase.isopen()) { log.d(tag, "database open"); } else { log.d(tag,"database closed"); } locationmanager = (locationmanager) getsystemservice(context.location_service); if (locationmanager.isproviderenabled(locationmanager.gps_provider)){ //toast.maketext(this, "gps enabled in devide", toast.length_short).show(); }else{ showgpsdisabledalerttouser(); } //mylocationlistener = new mylocationlistener(); notificationmanager = (notificationmanager)getsystemservice(notification_service); }
data source:
public class stopsdatasource { sqliteopenhelper dbhelper; sqlitedatabase database; public stopsdatasource(context context) { dbhelper = new stopsdbhelper(context, "stops", null, 1 ); } public void open() { database = dbhelper.getwritabledatabase(); } public void close() { dbhelper.close(); } public stops create(stops stops) { contentvalues values = new contentvalues(); values.put(stopsdbhelper.column_stop_lat, stops.getlat()); values.put(stopsdbhelper.column_stop_lon, stops.getlon()); values.put(stopsdbhelper.column_stop_name, stops.getname()); values.put(stopsdbhelper.column_stop_number,stops.getnumber()); long instertid = database.insert(stopsdbhelper.table_stops, null, values); stops.setid(instertid); return stops; } }
and db helper:
public class stopsdbhelper extends sqliteopenhelper { public static final string table_stops = "stops"; public static final string column_id = "stopid"; public static final string column_stop_name = "name"; public static final string column_stop_lat = "lat"; public static final string column_stop_lon = "lon"; public static final string column_stop_number = "number"; public static final string table_create = "create table if not exists " + table_stops + "(" + column_id + " integer primary key autoincrement, " + column_stop_lat + " text, " + column_stop_lon + " text, " + column_stop_number + " text, " + column_stop_name + " text" + ")"; public stopsdbhelper(context context, string name, sqlitedatabase.cursorfactory factory, int version) { super(context, name, factory, version); } @override public void oncreate(sqlitedatabase sqlitedatabase) { sqlitedatabase.execsql(table_create); } @override public void onupgrade(sqlitedatabase sqlitedatabase, int i, int i2) { sqlitedatabase.execsql("drop table if exists " + table_stops); oncreate(sqlitedatabase); } }
you calling method createdata()
before initializing datasource
. make before method call. ie, make initializations first before calling function may use it.
datasource = new stopsdatasource(this); if (boot == false) { createdata(); }
Comments
Post a Comment