-2
 public class asynctask extends AsyncTask <Void, String, Void > {
     String title;



     @Override
     protected Void doInBackground(Void... params) {
         try {
         Document document = Jsoup.connect(url).get();
         title = document.title();
         } catch (IOException e) {
             e.printStackTrace();
         }
         return null;
     }

     @Override
     protected void onPostExecute(Void result) {
     TextView campotesto = (TextView) findViewById(R.id.text);
     campotesto.setText(title);
 }
}}

Every time i run this get a nullpointer exception on the settext line and on the line in which i create the asynctask class

Acerbic
  • 100
  • 1
  • 9

1 Answers1

0

Pass this title from do in doInBackground() to onPostExecute();

Try This:

 public class asynctask extends AsyncTask <Void, String, String> 
 {
    String title;

    @Override
    protected String doInBackground(Void... params) 
   {
        try 
        {
             Document document = Jsoup.connect(url).get();
             title = document.title();
        } 
        catch (IOException e)
        {
             e.printStackTrace();
        }
     return title;
     }

     @Override
     protected void onPostExecute(String title)
     {
         TextView campotesto = (TextView) findViewById(R.id.text);
         campotesto.setText(title);
     }
}
Aayushi
  • 574
  • 3
  • 14