java - NullPointerException for getApplicationContext() in Delegate Class -


i have second class call methods handle app updating. there progress dialog in them have pass current application context constructor. code works fine when called directly in mainactivity oncreate(), breaks down when delegate code external class. what's going wrong?

method call in oncreate():

private appupdatehelper appupdatehelper = new appupdatehelper(getapplicationcontext());  @override     protected void oncreate(bundle savedinstancestate) {     appupdatehelper.handleappupdate();     } 

delegate class:

public class appupdatehelper {      private context mcontext;      public appupdatehelper(context mcontext) {         this.mcontext = mcontext;     }      public void handleappupdate() {         string versioncode = getversioncode(); // app's current version code          // app update date?         if (isappcurrent(versioncode)) {             promptappupdate();         }     }       private string getversioncode() {         string versioncode = null;         try {             packageinfo pinfo = mcontext.getpackagemanager().getpackageinfo(mcontext.getpackagename(), 0);             versioncode = pinfo.versionname;             // log.w(mcontext.getclass().getsimplename(), "current version: " + versioncode);         } catch (packagemanager.namenotfoundexception e) {             e.printstacktrace();         }         return versioncode;     }       private boolean isappcurrent(string versioncode) {         parseinstallation installation = parseinstallation.getcurrentinstallation();         string userappversion = installation.getstring("appversion");         return !userappversion.equals(versioncode);     }       private void promptappupdate() {         sweetalertdialog pdialog = new sweetalertdialog(mcontext, sweetalertdialog.warning_type);         pdialog.settitletext("update available!");         pdialog.setcontenttext("you must update continue using yeet club!");         pdialog.setconfirmclicklistener(sdialog -> {             final string apppackagename = mcontext.getpackagename(); // getpackagename() context or activity object             try {                 mcontext.startactivity(new intent(intent.action_view, uri.parse("market://details?id=" + apppackagename)));             } catch (activitynotfoundexception anfe) {                 mcontext.startactivity(new intent(intent.action_view, uri.parse("https://play.google.com/store/apps/details?id=" + apppackagename)));             }              sdialog.dismisswithanimation();         });         pdialog.setcancelable(false);         pdialog.showcancelbutton(false);         pdialog.show();     }  } 

exception:

08 - 20 15: 43: 43.874 21456 - 21456 / com.app.android e / androidruntime: fatal exception: main process: com.app.android, pid: 21456 java.lang.runtimeexception: unable instantiate activity componentinfo {  com.app.android / com.app.android.activity.mainactivity }: java.lang.nullpointerexception: attempt invoke virtual method 'android.content.context android.content.context.getapplicationcontext()' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java: 2458) @ android.app.activitythread.handlelaunchactivity(activitythread.java: 2613) @ android.app.activitythread.access$900(activitythread.java: 180) @ android.app.activitythread$h.handlemessage(activitythread.java: 1473) @ android.os.handler.dispatchmessage(handler.java: 111) @ android.os.looper.loop(looper.java: 207) @ android.app.activitythread.main(activitythread.java: 5710) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java: 900) @ com.android.internal.os.zygoteinit.main(zygoteinit.java: 761) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.content.context android.content.context.getapplicationcontext()' on null object reference @ android.content.contextwrapper.getapplicationcontext(contextwrapper.java: 117) @ com.app.android.activity.mainactivity. < init > (mainactivity.java: 77) @ java.lang.class.newinstance(native method) @ android.app.instrumentation.newactivity(instrumentation.java: 1072) @ android.app.activitythread.performlaunchactivity(activitythread.java: 2448) @ android.app.activitythread.handlelaunchactivity(activitythread.java: 2613)  @ android.app.activitythread.access$900(activitythread.java: 180)  @ android.app.activitythread$h.handlemessage(activitythread.java: 1473)  @ android.os.handler.dispatchmessage(handler.java: 111)  @ android.os.looper.loop(looper.java: 207)  @ android.app.activitythread.main(activitythread.java: 5710)  @ java.lang.reflect.method.invoke(native method)  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java: 900)  @ com.android.internal.os.zygoteinit.main(zygoteinit.java: 761)  

this simple.

you can context instance after called oncreate

private appupdatehelper appupdatehelper;  @override      protected void oncreate(bundle savedinstancestate) {      appupdatehelper = new appupdatehelper(getapplicationcontext());      appupdatehelper.handleappupdate();     } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -