I am creating an app in which i want to scrape google data with the help of jsoup and show it to text view in android studio.
But after doing some coding with the help of jsoup i am getting following error:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.yasht.cricketapp, PID: 11929
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:325)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.jsoup.nodes.Element.text()' on a null object reference
at com.example.yasht.cricketapp.Bottomnav.score_scrape.doInBackground(score_scrape.java:30)
at com.example.yasht.cricketapp.Bottomnav.score_scrape.doInBackground(score_scrape.java:13)
at android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
this is my jsoup code with async task :
public class score_scrape extends AsyncTask<Void,Void,Void> {
String words;
TextView score;
public score_scrape( TextView score){
this.score =score;
}
@Override
protected Void doInBackground(Void... voids) {
try {
Document doc = Jsoup.connect("https://www.google.com/search?q=india+vs+australia+3rd+odi+live+score").get();
Element element = doc.select("div[imspo_mh_cricket__score-major]").first();
words = element.text();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
score.setText(words);
}
}
I am using async task method which is declared i my main activity.
Comment for any further information.