0

I am getting crash in one of my projects. This issue is in my force update code.

> Caused by java.lang.NullPointerException: Attempt to invoke virtual
> method 'java.lang.String org.jsoup.nodes.Element.ownText()' on a null
> object reference
>        at com.****.android.****.activities.ForceUpdate.doInBackground(ForceUpdate.java:42)
>        at com.****.android.****.activities.ForceUpdate.doInBackground(ForceUpdate.java:23)
>        at android.os.AsyncTask$2.call(AsyncTask.java:295)
>        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
>        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
>        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
>        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
>        at java.lang.Thread.run(Thread.java:818)

my code is something like this

public class ForceUpdate extends AsyncTask<String,String,JSONObject> 

{

private String latestVersion;
private String currentVersion;
private Context context;
public ForceUpdate(String currentVersion, Context context){
    this.currentVersion = currentVersion;
    this.context = context;
}
@Override
protected JSONObject doInBackground(String... params) {

    try {
        latestVersion = 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 new JSONObject();
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
    if(latestVersion!=null){
        if(!currentVersion.equalsIgnoreCase(latestVersion)){
            // Toast.makeText(context,"update is available.",Toast.LENGTH_LONG).show();
            if(!(context instanceof LoginActivity)) {
                if(!((Activity)context).isFinishing()){
                    showForceUpdateDialog();
                }
            }
        }
    }

    super.onPostExecute(jsonObject);
}
public void showForceUpdateDialog(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context,
            R.style.Theme_AppCompat_Dialog));

    alertDialogBuilder.setTitle(context.getString(R.string.youAreNotUpdatedTitle));
    alertDialogBuilder.setMessage(context.getString(R.string.youAreNotUpdatedMessage) + " " + latestVersion + context.getString(R.string.youAreNotUpdatedMessage1));
    alertDialogBuilder.setCancelable(false);
    alertDialogBuilder.setPositiveButton(R.string.updates, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())));
            dialog.dismiss();

        }
    });
    alertDialogBuilder.show();
}}
Umair
  • 6,366
  • 15
  • 42
  • 50
Dubey.A
  • 89
  • 1
  • 1
  • 9
  • 3
    So whatever is supposed to come out of `first()` is null. Open the URL in a browser and inspect the HTML. Does the div you look for exist? Maybe it's a good idea to NOT chain all of this together and add a few checks here and there. This is brittle. If Google changes their HTML you're screwed. – SurfMan Mar 12 '18 at 09:39
  • 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) – frogatto Mar 12 '18 at 10:06

1 Answers1

0

We are also using Jsoup to get latest app version. For us it works fine. Below is my code -

        @Override
    protected String doInBackground(Void... voids) {
        try {
            latestVersion = Jsoup.connect(context.getString(R.string.play_store_url)
                    + BuildConfig.APPLICATION_ID)
                    .timeout(TIMEOUT)
                    .referrer(context.getString(R.string.google))
                    .get()
                    .select(context.getString(R.string.css_parser))
                    .first()
                    .ownText();
        } catch (Exception e) {
            return newVersion;
        }
    }

Only difference is, userAgent. Try removing it.

Nagesh Jatagond
  • 344
  • 2
  • 13
  • In my case it is still not working with the same exception as asked here..Please share your strings value used in the snippet. Please help i wasted 15 hours to resolve this issue. – Zafar Imam Mar 31 '18 at 14:37
  • play_store_url string value is - "https://play.google.com/store/apps/details?id=". Its displaying without https, please add https:// also – Nagesh Jatagond Apr 02 '18 at 04:34
  • @ZafarImam please let me know whether its working or not. By the way, Your app is released and its in playstore right? – Nagesh Jatagond Apr 02 '18 at 04:38
  • thanks for the response.. but it is not working at all, Yes my app is in playstore. I tested it with my app as well as other apps, but no use. – Zafar Imam Apr 02 '18 at 06:04