Jsoup Element中的NullPointer异常

时间:2015-03-18 09:04:44

标签: java android jsoup

我想在Android应用程序中显示报纸标题和新闻。所以一开始我创建了一个Java项目并完成了我的任务。这是我的java项目类

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/");
    }

}

但是当我尝试在Android中实现这个相同的项目时,这会在NewAge.java类中给出一个Nullpointer异常

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

这是我的错误日志

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)

你能否建议我可以修复此错误? 谢谢

1 个答案:

答案 0 :(得分:0)

如果您有NullPointerException

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

这意味着document为空。您必须查看connectToTheWebpage(baseUrl)方法(您未提供)。