0

I am trying to parse some html from a website. I connect to a website in an AsyncTask. I try to parse the retrieved HTML with JSOUP parser.

website= task.get();
            String items="";
            Document doc = Jsoup.parse(website);
            Element content = doc.getElementById("rtable");
            Elements links = content.getElementsByTag("tr");
            for (Element link : links) {
             items=items+ link.child(2).text()+link.child(3).text()+ link.child(0).getElementsByTag("a").attr("href");

            }
            mDateDisplay.setText(items);

If I parse the html in a normal java project it works fine but not in the Android project. I get a null pointer exception in the line:

Element content = doc.getElementById("rtable");

THe output of the error:

11-25 16:42:30.674: W/System.err(972): java.lang.NullPointerException
11-25 16:42:30.684: W/System.err(972):  at com.example.seminarska.Prevozi.onCreate(Prevozi.java:89)
11-25 16:42:30.684: W/System.err(972):  at android.app.Activity.performCreate(Activity.java:5008)
11-25 16:42:30.684: W/System.err(972):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-25 16:42:30.684: W/System.err(972):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-25 16:42:30.694: W/System.err(972):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-25 16:42:30.694: W/System.err(972):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-25 16:42:30.694: W/System.err(972):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-25 16:42:30.731: W/System.err(972):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 16:42:30.734: W/System.err(972):  at android.os.Looper.loop(Looper.java:137)
11-25 16:42:30.734: W/System.err(972):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-25 16:42:30.761: W/System.err(972):  at java.lang.reflect.Method.invokeNative(Native Method)
11-25 16:42:30.764: W/System.err(972):  at java.lang.reflect.Method.invoke(Method.java:511)
11-25 16:42:30.764: W/System.err(972):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-25 16:42:30.764: W/System.err(972):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-25 16:42:30.774: W/System.err(972):  at dalvik.system.NativeStart.main(Native Method)

Oh, the website I am trying to parse is this.

0

1 Answer 1

1
String url = "http://www.google.com";
List<String> images = new ArrayList<String>();
Document doc = Jsoup.connect(url).get();
Elements img = doc.select("img");
for (Element el : img)
{
    String imageUrl = el.attr("src");
    images.add(imageUrl);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Error on line at 89: com.example.seminarska.Prevozi.onCreate(Prevozi.java:89)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.