Sunday 15 September 2013

java - Getting NullPointerException while setting SharedPreferences -



java - Getting NullPointerException while setting SharedPreferences -

i'm trying set sharedprefencesin onpostexecute method nullpointerexception in error log. i've tried set doinbackground() blocks result returned. code

my task class

public class logintask extends asynctask<void, void, string> { private final string memail; private final string mpassword; private sharedpreferences sharedpreferences; private final string mypreferences = "session" ; string url = **** logintask(string email, string password) { memail = email; mpassword = password; } @targetapi(build.version_codes.honeycomb) @override public string doinbackground(void... params) { // todo: effort authentication against network service. seek { // simulate network access. thread.sleep(2000); } grab (interruptedexception e) { homecoming "false"; } arraylist<basicnamevaluepair> namevaluepairs = new arraylist<basicnamevaluepair>(); namevaluepairs.add(new basicnamevaluepair("email", memail)); namevaluepairs.add(new basicnamevaluepair("password", mpassword)); //prepare post query seek { httpclient clienthttp = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = clienthttp.execute(httppost); httpentity entity = response.getentity(); inputstream = entity.getcontent(); bufferedreader reader = new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8); stringbuilder sb = new stringbuilder(); string line = reader.readline(); sb.append(line + "\n"); is.close(); string result = sb.tostring(); system.out.println(result); homecoming result; }catch (exception e){ homecoming null; } } @override protected void onpostexecute(final string res) { seek { jobj = new jsonobject(res); } grab (jsonexception e) { e.printstacktrace(); } seek { if (jobj.getstring("code").equals("1")) { sharedpreferences.editor e = sharedpreferences.edit(); e.putstring(mypreferences,"active"); e.commit(); intent myintent = new intent(getactivity(), homesactivity.class); //lançer l'activité startactivityforresult(myintent, 0); } else { //password.seterror(getstring(r.string.error_incorrect_password)); //password.requestfocus(); alertdialog.builder builder = new alertdialog.builder(getactivity()); builder.settitle("error !"); builder.setmessage("the info entered incorrect.\nplease seek again!") .setcancelable(false) .setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { dialog.cancel(); } }); alertdialog alert = builder.create(); alert.show(); } } grab (jsonexception e) { e.printstacktrace(); } } @override protected void oncancelled() { logintask = null; //showprogress(false); } }

my error log :

10-09 12:58:50.088 2878-2878/com.example.user.unchained e/androidruntime﹕ fatal exception: main process: com.example.user.unchained, pid: 2878 java.lang.nullpointerexception @ com.example.user.unchained.emailloginactivity$logintask.onpostexecute(emailloginactivity.java:328) @ com.example.user.unchained.emailloginactivity$logintask.onpostexecute(emailloginactivity.java:255) @ android.os.asynctask.finish(asynctask.java:632) @ android.os.asynctask.access$600(asynctask.java:177) @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:645) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5017) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) @ dalvik.system.nativestart.main(native method)

sharedpreferences null. declare here

private sharedpreferences sharedpreferences;

but never initialize (give value) before trying utilize here

sharedpreferences.editor e = sharedpreferences.edit();

before line, need

sharedpreferences = getsharedpreferences("mypref", 0);

but need context. so, if separate class need pass context of activity asynctask constructor.

so, need create class variable context mcontext; alter constructor to

logintask(string email, string password, context c) { memail = email; mpassword = password; mcontext = c; }

then when calling task, pass activity context along email , password this or activityname.this, or else depending on how/where called.

now, when initialize sharedpreference object be

sharedpreferences = mcontext.getsharedpreferences("mypref", 0);

java android sharedpreferences

No comments:

Post a Comment