although the concern is somewhat related to null as the ownText() returns null that's fine and can be handle the main-concern is the approach used is not well enough i am checking div[itemprop=softwareVersion
.i have change the version checking with the answer given below which is just checking the softwareVersion for version on the playstore .
Original Question I have a live app which was working fine few days back ,now have a concern as when the user open app from the Play store or from app drawer or from home screen shortcut .app is getting crashed randomly and out 1/5 it is launching fine
Below is the version checker code
build.gradle(app level)
compile 'org.jsoup:jsoup:1.8.3'
java code:
public class VersionChecker extends AsyncTask<String, String, String> {
String newVersion;
Context context;
public VersionChecker(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... params) {
try {
newVersion = Jsoup.connect("https://play.google.com/store /apps/details?id=" + context.getPackageName() + "&hl=en")
.timeout(30000)
.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
.referrer("http://www.google.com")
.get()
.select("div[itemprop=softwareVersion]")
.first()
.ownText();
} catch (IOException e) {
e.printStackTrace();
}
return newVersion;
} }
and below is the exception trace
Exception: FATAL EXCEPTION: main Process: com.demo.android.myapp, PID: 5325 java.lang.RuntimeException: Unable to resume activity {com.demo.android.myapp/com.demo.android.myapp.SplashActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference at com.demo.android.myapp.SplashActivity.checkVersion(SplashActivity.java:81) at com.demo.android.myapp.SplashActivity.onResume(SplashActivity.java:242) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258) at android.app.Activity.performResume(Activity.java:6327) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)