java - App throwing NullException error when Initializng variables -
currently app pulling database information , populating listview held within tabview, app force closes before can run.
the problem appears lie when initializing variables.
below the oncreate class , intializevariables method logcat showing error:
protected void oncreate(bundle savedinstancestate) { // set activity full screen!! requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); th = (tabhost) findviewbyid(r.id.tabhost); th.setup(); tabspec specs = th.newtabspec("tag1"); // 1 tab specs.setcontent(r.id.tab1); // appears on actual tab specs.setindicator("tools"); th.addtab(specs); specs = th.newtabspec("tag2"); // 1 tab specs.setcontent(r.id.tab2); // appears on actual tab specs.setindicator("calorie calculator"); th.addtab(specs); specs = th.newtabspec("tag3"); // 1 tab specs.setcontent(r.id.tab3); // appears on actual tab specs.setindicator("your stats!"); th.addtab(specs); initializevariables(); statlistview = getlistview(); statlistview.setonitemclicklistener(statviewlistener); // map each name textview string[] = new string[] { "name" }; int[] = new int[] { r.id.tvstatistics }; statadapter = new simplecursoradapter(mainactivity.this, r.layout.stat_list, null, from, to); setlistadapter(statadapter); // set adapter arrayadapter<charsequence> adapter = arrayadapter.createfromresource( this, r.array.spinactmultarray, android.r.layout.simple_spinner_item); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinactmult.setadapter(adapter); spinactmult.setonitemselectedlistener(new spinactmultfunction()); } private void initializevariables() { spinactmult = (spinner) findviewbyid(r.id.spinactivitymultipler); activitymultiplier = (textview) findviewbyid(r.id.tvactmult); maintcalories = (textview) findviewbyid(r.id.tvmaintlevel); calccalories = (button) findviewbyid(r.id.btncalccalories); goalpicker = (radiogroup) findviewbyid(r.id.rggoalpicker); calorieresult = (textview) findviewbyid(r.id.tvcalresults); lbmresult = (textview) findviewbyid(r.id.tvlbmresult); bmrresult = (textview) findviewbyid(r.id.tvbmrresult); etweight = (edittext) findviewbyid(r.id.etwinput); weightint = etweight.gettext().tostring(); etbodyfat = (edittext) findviewbyid(r.id.etbfinput); bodyfatint = etbodyfat.gettext().tostring(); goalpicker.setoncheckedchangelistener(this); calccalories.setonclicklistener(this); stopwatchactivity = (button) findviewbyid(r.id.btnstopwatchactivity); maparun = (button) findviewbyid(r.id.btnmaparun); weightconverter = (button) findviewbyid(r.id.btnweightconverter); maparun.setonclicklistener(this); weightconverter.setonclicklistener(this); stopwatchactivity.setonclicklistener(this); updatedb.setonclicklistener(this); viewdb.setonclicklistener(this); }
and logcat:
05-10 03:41:52.687: e/androidruntime(5974): fatal exception: main 05-10 03:41:52.687: e/androidruntime(5974): java.lang.runtimeexception: unable start activity componentinfo{com.uhi.fatfighter/com.uhi.fatfighter.mainactivity}: java.lang.nullpointerexception 05-10 03:41:52.687: e/androidruntime(5974): @ android.app.activitythread.performlaunchactivity(activitythread.java:2185) 05-10 03:41:52.687: e/androidruntime(5974): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2210) 05-10 03:41:52.687: e/androidruntime(5974): @ android.app.activitythread.access$600(activitythread.java:142) 05-10 03:41:52.687: e/androidruntime(5974): @ android.app.activitythread$h.handlemessage(activitythread.java:1208) 05-10 03:41:52.687: e/androidruntime(5974): @ android.os.handler.dispatchmessage(handler.java:99) 05-10 03:41:52.687: e/androidruntime(5974): @ android.os.looper.loop(looper.java:137) 05-10 03:41:52.687: e/androidruntime(5974): @ android.app.activitythread.main(activitythread.java:4928) 05-10 03:41:52.687: e/androidruntime(5974): @ java.lang.reflect.method.invokenative(native method) 05-10 03:41:52.687: e/androidruntime(5974): @ java.lang.reflect.method.invoke(method.java:511) 05-10 03:41:52.687: e/androidruntime(5974): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:791) 05-10 03:41:52.687: e/androidruntime(5974): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:558) 05-10 03:41:52.687: e/androidruntime(5974): @ dalvik.system.nativestart.main(native method) 05-10 03:41:52.687: e/androidruntime(5974): caused by: java.lang.nullpointerexception 05-10 03:41:52.687: e/androidruntime(5974): @ com.uhi.fatfighter.mainactivity.initializevariables(mainactivity.java:129) 05-10 03:41:52.687: e/androidruntime(5974): @ com.uhi.fatfighter.mainactivity.oncreate(mainactivity.java:86) 05-10 03:41:52.687: e/androidruntime(5974): @ android.app.activity.performcreate(activity.java:5008) 05-10 03:41:52.687: e/androidruntime(5974): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1079) 05-10 03:41:52.687: e/androidruntime(5974): @ android.app.activitythread.performlaunchactivity(activitythread.java:2139) 05-10 03:41:52.687: e/androidruntime(5974): ... 11 more
in here:
updatedb.setonclicklistener(this); //<<<< viewdb.setonclicklistener(this); //<<<<
you forget initialize updatedb
, viewdb
instances before using inside initializevariables()
method
Comments
Post a Comment