0
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.jsoup.nodes.Element.ownText()' on a null object reference

Here is My Code:

public class VersionChecker extends AsyncTask<String, String, String> {

    private String newVersion;

    @Override
    protected String doInBackground(String... params) {

        try {
            newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID + "&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;
    }
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
  • 5
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – takendarkk Apr 09 '18 at 13:49
  • 'first()' is returning null which it can if prior 'select' produced no results (empty list). –  Apr 09 '18 at 16:08

2 Answers2

0

Try doing something like this.( Not the right way around but just a temporary fix )

val versionUrl =
            "https://play.google.com/store/apps/details?id=" + context.packageName + "&hl=en"
        latestVersion = Jsoup.connect(versionUrl)
            .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()
            .html()
            .replaceAfter("<!", "")
            .replace("<!", "")
-1

Android playstore updated their UI, so trying to select the current version from the unparsed html is returning null cause it no longer exists. The "version" tag we parsed for now exists inside this popup

I'll suggest an alternative:

You'll have to choose a different method of detecting an update, I'd recommend checking the in-app API from Android.

https://developer.android.com/guide/playcore/in-app-updates/kotlin-java#groovy

  1. You'll just need to add the dependency and resync gradle files:

implementation 'com.google.android.play:app-update-ktx:2.0.0'

  1. Implement the AppUpdateManger snippet available in the doc linked above, i've wrapped mine in a method that returns a boolean as to if an update is available or not:

    public boolean isUpdateAvailable() {
    
        AtomicReference<Boolean> isUpdateAvailable = new AtomicReference<>(false);
        AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(context);
    
        // Returns an intent object that you use to check for an update.
        Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
    
        // Checks that the platform will allow the specified type of update.
        appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
    
            /** UpdateAvailability
             * int UNKNOWN = 0;
             * int UPDATE_NOT_AVAILABLE = 1;
             * int UPDATE_AVAILABLE = 2;
             * int DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS = 3;
             * */
    
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
                isUpdateAvailable.set(true);
            }
        });
    
        return isUpdateAvailable.get();
    }
    
  • Pls share such info in comments and not answers. Answers are only used when you have a solution to the problem. Comments can be used for clarification or telling such info. – Archit Gargi Jun 04 '22 at 03:09