gpt4 book ai didi

java - Jsoup 元素中的 NullPointer 异常

转载 作者:太空宇宙 更新时间:2023-11-04 14:03:10 24 4
gpt4 key购买 nike

我想在 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)

你能建议我修复这个错误吗?谢谢

最佳答案

如果

上出现 NullPointerException
Elements elements = document.select("h2 a");

这意味着文档为空。您必须查看 connectToTheWebpage(baseUrl) 方法(您没有提供)。

关于java - Jsoup 元素中的 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29117815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com