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

匿名 (未验证) 提交于 2019-12-03 02:35:01

问题:

I am using below code to get versionName from playstore by using jsoup I am fetching details but its throwing some exception.

My code is

public class ForceUpdateAsync extends AsyncTask<String, String, JSONObject>{  private String latestVersion; private String currentVersion; private Context context; public ForceUpdateAsync(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 SplashActivity)) {                 if(!((Activity)context).isFinishing()){                     showForceUpdateDialog();                 }             }         }     }     super.onPostExecute(jsonObject); }  public void showForceUpdateDialog(){     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context,             R.style.DialogDark));      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.update, 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.cancel();         }     });     alertDialogBuilder.show(); } } 

but I am getting null pointer exception error

Please some one help me to fix this issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!