简体   繁体   中英

NullPointer exception in Jsoup Element

I want to show newspapers headline and news in android application. So at first I create a Java project and did my task perfectly. here is my java projects classes

Main.java

package com.emon.newsportal;

import java.io.ObjectInputStream.GetField;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        newspaperTest(new NewAge());
    }

    private static void newspaperTest(Newspaper newspaper) {
        List<Headline>headlines=newspaper.getAllSportsHeadLines();

        System.out.println(newspaper.getSportsNews(headlines.get(0)));
        System.out.println(headlines.get(0).getHeading());
    }


}

NewAge.java

package com.emon.newsportal;



import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class NewAge extends Newspaper{

    protected List<Headline> getHeadlines(String baseUrl) {
        List<Headline> headlines = new ArrayList<Headline>();

        Document document = connectToTheWebpage(baseUrl);

        Elements elements = document.select("h2 a");


        for (Element element : elements) {

            Headline headline = new Headline(element.text(),
                    element.attr("href"));
            headlines.add(headline);

        }
//      System.out.println(""+headlines);
        return headlines;
    }

    protected String getNews(Headline headline) {
        Document document = connectToTheWebpage(headline.getUrl());

        return document.select("body p").get(0).text();
    }

    @Override
    public List<Headline> getAllSportsHeadLines() {
        // TODO Auto-generated method stub
        return getHeadlines("http://newagebd.net/category/sport/");
    }



    @Override
    public List<Headline> getALLPoliticalHeadlines() {
        // TODO Auto-generated method stub
        return getHeadlines("http://newagebd.net/category/politics/");
    }



    @Override
    public List<Headline> getALLEntertainmentHeadlines() {
        // TODO Auto-generated method stub
        return getHeadlines("http://newagebd.net/category/entertain/");
    }


    @Override
    public List<Headline> getALLBusinessHeadlines() {
        // TODO Auto-generated method stub
        return getHeadlines("http://newagebd.net/category/bizn/");
    }

}

But when I try to implement this same project in Android this gives a Nullpointer exception in the NewAge.java class

Elements elements = document.select("h2 a");

here is my error log

03-18 14:49:53.974    1056-1070/com.songkeet.news_a W/System.err﹕ java.lang.NullPointerException
03-18 14:49:53.974    1056-1070/com.songkeet.news_a W/System.err﹕ at com.songkeet.news_a.NewAge.getHeadlines(NewAge.java:27)
03-18 14:49:53.974    1056-1070/com.songkeet.news_a W/System.err﹕ at com.songkeet.news_a.NewAge.getAllSportsHeadLines(NewAge.java:50)
03-18 14:49:53.974    1056-1070/com.songkeet.news_a W/System.err﹕ at com.songkeet.news_a.MainActivity$1.run(MainActivity.java:36)
03-18 14:49:53.984    1056-1070/com.songkeet.news_a W/System.err﹕ at     java.lang.Thread.run(Thread.java:856)

Can you suggest me I can I fix this error? Thank you

If you have a NullPointerException on

Elements elements = document.select("h2 a");

It means the document is null. You have to look at the connectToTheWebpage(baseUrl) method (you don't provide).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM