java - What exactly is causing this NullPointerException? -
this question has answer here:
i'm pretty new android development , working on app supposed save current time , date when button pressed, store sharedpreferences , present string when different button pressed. got working did tinkering today , i'm getting nullpointerexception can't solve, after having spent 45 minutes on google , stackoverflow trying different code fragments. can better @ me tell me i'm missing?
here relevant code:
public class gettimestamp extends appcompatactivity { private string date; public void setdate (view view){ dateformat df = new simpledateformat("dd.mm.yyy 'om' hh:mm"); date = df.format(calendar.getinstance().gettime()); } public string getdate(){ return date; } sharedpreferences prefs = this.getsharedpreferences("sharedprefs", context.mode_private); sharedpreferences.editor editor = prefs.edit(); protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_get_time_stamp); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fabsavetimestamp); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { setdate(view); editor.putstring("storeddate", date); editor.commit(); toast.maketext(getbasecontext(), "blablabla", toast.length_short).show(); } }) floatingactionbutton fab3 = (floatingactionbutton) findviewbyid(r.id.fabshowtimestamp); fab3.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { //retrieve date sharedpreferences , store local string string retrieveddate = prefs.getstring("storeddate", null); if (retrieveddate!=null){ alertdialog alertdialog = new alertdialog.builder(gettimestamp.this).create(); //read update alertdialog.settitle(""); alertdialog.setmessage("somemessage" + retrieveddate); alertdialog.setbutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { } }); alertdialog.show(); }else { //create alert dialog explaining user no date has been saved yet alertdialog alertdialog = new alertdialog.builder(gettimestamp.this).create(); //read update alertdialog.settitle("error"); alertdialog.setmessage("errorexplanation"); alertdialog.setbutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { } }); alertdialog.show(); } } }); and here errors:
02-08 22:49:36.142 19735-19735/? w/dalvikvm: threadid=1: thread exiting uncaught exception (group=0x41ed7ce0) 02-08 22:49:36.152 19735-19735/? e/androidruntime: fatal exception: main process: com.example.user.appname, pid: 19735 java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.user.appname/com.example.user.appname.gettimestamp}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2131) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2264) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1205) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5139) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:796) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:612) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ android.content.contextwrapper.getsharedpreferences(contextwrapper.java:173) @ com.example.user.appname.gettimestamp.<init>(gettimestamp.java:36) @ java.lang.class.newinstanceimpl(native method) @ java.lang.class.newinstance(class.java:1208) @ android.app.instrumentation.newactivity(instrumentation.java:1061) @ android.app.activitythread.performlaunchactivity(activitythread.java:2122) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2264) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1205) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5139) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:796) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:612) @ dalvik.system.nativestart.main(native method)
you trying initialize following class field before allowed to:
sharedpreferences prefs = this.getsharedpreferences("sharedprefs", context.mode_private); only declare field:
sharedpreferences prefs; and assign in oncreate() method:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); prefs = getsharedpreferences("sharedprefs", context.mode_private); ... }
Comments
Post a Comment