java - Getting NullPointerException while skipping login Activity -


i want skip login activity using shared preferences whenever try implement code java.lang.nullpointerexception error occur. java code of launch or login screen. here have no splash screen.

public class mainactivity extends activity {     button sub;     sharedpreferences prefs;     edittext useret,pwdet;     string is_login = "isloggedin";      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         if(this.isloggedin()==true) {             checklogin();         }         setcontentview(r.layout.activity_main);         useret=(edittext) findviewbyid(r.id.etuser);         pwdet=(edittext) findviewbyid(r.id.etpwd);         sub=(button) findviewbyid(r.id.submit);         sub.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 string username=useret.gettext().tostring();                 string password=pwdet.gettext().tostring();                 prefs=getsharedpreferences("codelearn_twitter", mode_private);                 sharedpreferences.editor editor=prefs.edit();                 editor.putstring("key_for_username",username);                 editor.putstring("key_for_passwrd",password);                 editor.putboolean(is_login, true);                 editor.commit();                  intent i=new intent(mainactivity.this,listactivity.class);                 startactivity(i);             }         });     }     public void checklogin(){         if(this.isloggedin()==true){             intent i=new intent(mainactivity.this,listactivity.class);             startactivity(i);             finish();         }     }     public boolean isloggedin(){         return prefs.getboolean(is_login, false);     } } 

below list of error..

08-19 17:58:17.061 14978-14978/com.example.viren.codelearn
e/androidruntime﹕ fatal exception: main process: com.example.viren.codelearn, pid: 14978 java.lang.runtimeexception: unable start activity componentinfo {com.example.viren.codelearn/com.example.vire n.codelearn.mainactivity}:java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity (activitythread.java:2305) @ android.app.activitythread.handlelaunchactivity (activitythread.java:2363) @ android.app.activitythread.access$900(activitythread.java:161) @ android.app.activitythread$h.handlemessage (activitythread.java:1265) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:157) @ android.app.activitythread.main(activitythread.java:5356) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run (zygoteinit.java:1265) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) @ de.robv.android.xposed.xposedbridge.main(xposedbridge.java:132) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.example.viren.codelearn.mainactivity.isloggedin (mainactivity.java:56) @ com.example.viren.codelearn.mainactivity.oncreate (mainactivity.java:23) @ android.app.activity.performcreate(activity.java:5426) @ android.app.instrumentation.callactivityoncreate (instrumentation.java:1105) @ android.app.activitythread.performlaunchactivity (activitythread.java:2269)  at android.app.activitythread.handlelaunchactivity (activitythread.java:2363)

   
           

looking @ stacktrace, can see @ line 56 call isloggedin calls prefs variable, not initialized yet (it's null), getting null pointer exception.

make prefs variable accesible whole class, , initialize before calling isloggedin or initialize inside isloggedin method.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -