android - nullpointerexception getSharedPreferences -
i trying save preferences within android app. when trying sharedpreference getting nullpointerexception. error:
04-25 13:58:44.814: w/system.err(22795): java.lang.nullpointerexception 04-25 13:58:44.824: w/system.err(22795): @ com.koeriers.standaard.sharedpreferences.setdata(sharedpreferences.java:27) 04-25 13:58:44.829: w/system.err(22795): @ com.koeriers.standaard.actlogin.oncreate(actlogin.java:79) 04-25 13:58:44.834: w/system.err(22795): @ android.app.activity.performcreate(activity.java:5206) 04-25 13:58:44.839: w/system.err(22795): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1094) 04-25 13:58:44.844: w/system.err(22795): @ android.app.activitythread.performlaunchactivity(activitythread.java:2074) 04-25 13:58:44.849: w/system.err(22795): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2135) 04-25 13:58:44.854: w/system.err(22795): @ android.app.activitythread.access$700(activitythread.java:140) 04-25 13:58:44.859: w/system.err(22795): @ android.app.activitythread$h.handlemessage(activitythread.java:1237) 04-25 13:58:44.864: w/system.err(22795): @ android.os.handler.dispatchmessage(handler.java:99) 04-25 13:58:44.869: w/system.err(22795): @ android.os.looper.loop(looper.java:137) 04-25 13:58:44.874: w/system.err(22795): @ android.app.activitythread.main(activitythread.java:4921) 04-25 13:58:44.874: w/system.err(22795): @ java.lang.reflect.method.invokenative(native method) 04-25 13:58:44.879: w/system.err(22795): @ java.lang.reflect.method.invoke(method.java:511) 04-25 13:58:44.884: w/system.err(22795): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1027) 04-25 13:58:44.889: w/system.err(22795): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:794) 04-25 13:58:44.894: w/system.err(22795): @ dalvik.system.nativestart.main(native method)
this sharedpreferences class:
package com.myapp.standaard; import android.os.bundle; import android.preference.preferenceactivity; import android.widget.edittext; public class sharedpreferences extends preferenceactivity { public static string guid = "8bb4894b-92f7-45af-9a40-b99d7a06a506"; public static string licensekey = "letmein"; edittext etlicense; android.content.sharedpreferences sharedprefs; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); try { addpreferencesfromresource(r.xml.preferences); etlicense = (edittext) findviewbyid(r.id.etlicense); } catch (exception e) { e.printstacktrace(); } } public boolean setdata(string setpreference, string setvalue) { android.content.sharedpreferences.editor editor = sharedprefs.edit(); editor.putstring(setpreference, setvalue); editor.commit(); return true; } public string getdata(string getpreference, string errormessage) { sharedprefs = getsharedpreferences(getpreference, mode_private); string returndata = sharedprefs.getstring(getpreference, errormessage); return returndata; } }
and place in activity trying retrieve value preference:
try{ sharedpreferences sp = new sharedpreferences(); licentie = sp.getdata("licensekey", "error loading license"); } catch (exception e){ e.printstacktrace(); licentie = ""; }
you have'nt called public boolean
setdata(string setpreference, string setvalue)
anywhere why on licentie = sp.getdata("licensekey", "error loading license");
it giving nullpointerexception
.
first set "licensekey" try retrieve it.
note -- kind of implementation used according me not correct.