java - NullPointException Virtual Method -


when run app, shows logcat :-

07-13 17:31:10.418: e/androidruntime(20063): process: com.quickblox.q_municate, pid: 20063 07-13 17:31:10.418: e/androidruntime(20063): java.lang.runtimeexception: unable instantiate activity componentinfo{com.quickblox.q_municate/ui.splash.splashactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string android.content.context.getpackagename()' on null object reference 07-13 17:31:10.418: e/androidruntime(20063):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2240) 07-13 17:31:10.418: e/androidruntime(20063):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2389) 07-13 17:31:10.418: e/androidruntime(20063):    @ android.app.activitythread.access$900(activitythread.java:147) 07-13 17:31:10.418: e/androidruntime(20063):    @ android.app.activitythread$h.handlemessage(activitythread.java:1296) 07-13 17:31:10.418: e/androidruntime(20063):    @ android.os.handler.dispatchmessage(handler.java:102) 07-13 17:31:10.418: e/androidruntime(20063):    @ android.os.looper.loop(looper.java:135) 07-13 17:31:10.418: e/androidruntime(20063):    @ android.app.activitythread.main(activitythread.java:5254) 07-13 17:31:10.418: e/androidruntime(20063):    @ java.lang.reflect.method.invoke(native method) 07-13 17:31:10.418: e/androidruntime(20063):    @ java.lang.reflect.method.invoke(method.java:372) 07-13 17:31:10.418: e/androidruntime(20063):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:898) 07-13 17:31:10.418: e/androidruntime(20063):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:693) 07-13 17:31:10.418: e/androidruntime(20063): caused by: java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string android.content.context.getpackagename()' on null object reference 07-13 17:31:10.418: e/androidruntime(20063):    @ android.content.contextwrapper.getpackagename(contextwrapper.java:137) 07-13 17:31:10.418: e/androidruntime(20063):    @ com.quickblox.q_municate_core.utils.prefshelper.<init>(prefshelper.java:48) 07-13 17:31:10.418: e/androidruntime(20063):    @ ui.splash.splashactivity.<init>(splashactivity.java:38) 07-13 17:31:10.418: e/androidruntime(20063):    @ java.lang.reflect.constructor.newinstance(native method) 07-13 17:31:10.418: e/androidruntime(20063):    @ java.lang.class.newinstance(class.java:1572) 07-13 17:31:10.418: e/androidruntime(20063):    @ android.app.instrumentation.newactivity(instrumentation.java:1065) 07-13 17:31:10.418: e/androidruntime(20063):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2230) 

and code:-

splashactivity:-

public class splashactivity extends baseactivity {  private static final string tag = splashactivity.class.getsimplename(); private facebookhelper facebookhelper; public string useremail; public string userpassword; public prefshelper prefshelper= new prefshelper(this); public static void start(context context) {     intent intent = new intent(context, splashactivity.class);     context.startactivity(intent); }  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      addactions();      facebookhelper = new facebookhelper(this, savedinstancestate, new facebooksessionstatuscallback());      useremail = prefshelper.getpref(prefshelper.pref_user_email);     userpassword = prefshelper.getpref(prefshelper.pref_user_password);      boolean isrememberme = prefshelper.getpref(prefshelper.pref_remember_me, false);      if (isrememberme) {         checkstartexistsession(useremail, userpassword);     } else {         startlanding();     }      setcontentview(r.layout.activity_splash); }  @override public void onstart() {     super.onstart();     facebookhelper.onactivitystart(); }  @override protected void onresume() {     super.onresume();     if (isloggedintochat()) {         startmainactivity();         finish();     } }  @override public void onstop() {     super.onstop();     facebookhelper.onactivitystop(); }  @override protected void onfailaction(string action) {     super.onfailaction(action);     startlanding(); }  private boolean isloggedintochat() {     return qbchatservice.isinitialized() && qbchatservice.getinstance().isloggedin(); }  private void checkstartexistsession(string useremail, string userpassword) {     boolean isemailentered = !textutils.isempty(useremail);     boolean ispasswordentered = !textutils.isempty(userpassword);     if ((isemailentered && ispasswordentered) || (isloggedviafb(ispasswordentered))) {         runexistsession(useremail, userpassword);     } else {         startlanding();     } }  private boolean isloggedviafb(boolean ispasswordentered) {     return ispasswordentered && logintype.facebook.equals(getcurrentlogintype()); }  private void addactions() {     addaction(qbserviceconsts.login_success_action, new loginsuccessaction());     addaction(qbserviceconsts.login_and_join_chats_fail_action, failaction);     addaction(qbserviceconsts.login_fail_action, failaction); }  public boolean isloggedviafb() {     return facebookhelper.issessionopened() && logintype.facebook.equals(getcurrentlogintype()); }  @override protected void onsaveinstancestate(bundle outstate) {     super.onsaveinstancestate(outstate);     facebookhelper.onsaveinstancestate(outstate); }  @override public void onactivityresult(int requestcode, int resultcode, intent data) {     super.onactivityresult(requestcode, resultcode, data);     facebookhelper.onactivityresult(requestcode, resultcode, data); }  private void startlanding() {     landingactivity.start(splashactivity.this);     finish(); }  private void runexistsession(string useremail, string userpassword) {     //check token valid 1 minute     if (appsession.issessionexistornotexpired(timeunit.minutes.tomillis(             constscore.token_valid_time_in_minutes))) {         startmainactivity();         finish();     } else {         doautologin(useremail, userpassword);     } }  private void doautologin(string useremail, string userpassword) {     if (logintype.email.equals(getcurrentlogintype())) {         login(useremail, userpassword);     } else {         facebookhelper.logout();         facebookhelper.loginwithfacebook();     } }  private void login(string useremail, string userpassword) {     qbuser user = new qbuser(null, userpassword, useremail);     qblogincommand.start(this, user); }  private logintype getcurrentlogintype() {     return appsession.getsession().getlogintype(); }  private void startmainactivity() {     prefshelper.savepref(prefshelper.pref_import_initialized, true);     mainactivity.start(splashactivity.this); }  private class facebooksessionstatuscallback implements session.statuscallback {      @override     public void call(session session, sessionstate state, exception exception) {         if (session.isopened() && logintype.facebook.equals(getcurrentlogintype())) {             qbloginrestwithsocialcommand.start(splashactivity.this, qbprovider.facebook,                     session.getaccesstoken(), null);         }     } }  private class loginsuccessaction implements command {      @override     public void execute(bundle bundle) {         qbuser user = (qbuser) bundle.getserializable(qbserviceconsts.extra_user);          startmainactivity();          analyticsutils.pushanalyticsdata(splashactivity.this, user, "user sign in");          finish();     } } 

prefshelper:-

public class prefshelper {      public static final string pref_remember_me = "remember_me";     public static final string pref_login_type = "login_type";     public static final string pref_dialog_id = "dialog_id";     public static final string pref_user_email = "email";     public static final string pref_user_id = "user_id";     public static final string pref_is_logined = "is_logined";     public static final string pref_user_password = "password";     public static final string pref_push_notifications = "push_notifications";     public static final string pref_import_initialized = "import_initialized";     public static final string pref_user_learned_drawer = "navigation_drawer_learned";     public static final string pref_sign_up_initialized = "sign_up_initialized";     public static final string pref_missed_message = "missed_message";     public static final string pref_status = "status";     public static final string pref_push_message = "message";     public static final string pref_reg_user_id = "registered_push_user";     public static final string pref_user_agreement = "user_agreement";     public static final string pref_joined_to_all_dialogs = "joined_to_all_dialogs";      public static final string pref_push_message_need_to_open_dialog = "push_need_to_open_dialog";     public static final string pref_push_message_dialog_id = "push_dialog_id";     public static final string pref_push_message_user_id = "push_user_id";      public static final string pref_reg_id = "registration_id";     public static final string pref_app_version = "appversion";     public static final string pref_receive_push = "receive_push";     public static final string pref_is_subscribed_on_server = "subscribed_on_server";     public static final string pref_user_full_name = "full_name";     public static final string pref_session_token = "session_token";      private final sharedpreferences sharedpreferences;     private final sharedpreferences.editor editor;      private static prefshelper instance;      public static prefshelper getprefshelper() {         return instance;     }      public prefshelper(context context) {         instance = this;         string prefsfile = context.getpackagename();         sharedpreferences = context.getsharedpreferences(prefsfile, context.mode_private);         editor = sharedpreferences.edit();     }      public void delete(string key) {         if (sharedpreferences.contains(key)) {             editor.remove(key).commit();         }     }      public void savepref(string key, object value) {         delete(key);          if (value instanceof boolean) {             editor.putboolean(key, (boolean) value);         } else if (value instanceof integer) {             editor.putint(key, (integer) value);         } else if (value instanceof float) {             editor.putfloat(key, (float) value);         } else if (value instanceof long) {             editor.putlong(key, (long) value);         } else if (value instanceof string) {             editor.putstring(key, (string) value);         } else if (value instanceof enum) {             editor.putstring(key, value.tostring());         } else if (value != null) {             throw new runtimeexception("attempting save non-primitive preference");         }          editor.commit();     }      @suppresswarnings("unchecked")     public <t> t getpref(string key) {         return (t) sharedpreferences.getall().get(key);     }      @suppresswarnings("unchecked")     public <t> t getpref(string key, t defvalue) {         t returnvalue = (t) sharedpreferences.getall().get(key);         return returnvalue == null ? defvalue : returnvalue;     }      public boolean isprefexists(string key) {         return sharedpreferences.contains(key);     } } 

you have declared public prefshelper prefshelper; , did not instantiate prefshelper that's why getting nullpointerexception when accessing

     useremail = prefshelper.getpref(prefshelper.pref_user_email); 

instantiate prefshelper, should solve problem.

       prefshelper = new prefshelper(this.getapplicationcontext()); 

update

i think problem instantiating prefshelper before oncreate(). can define there, don't instantiate it

prefshelper prefshelper; 

inside oncreate(), after

super.oncreate(savedinstancestate); 

do this

prefshelper = new prefshelper(this.getapplicationcontext()); 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -