使用 JSOUP 强制关闭 nullPointerException 解析标签
尝试解析链接时
http://pc.gamespy.com/pc/bastion/
使用
Element overview = doc.select("div#object-overview").last();
Element paragraph = overview.select("p").last();
它给了我一个空指针异常。
还有这个
http://wii.gamespy.com/wii /jerry-rice-nitus-dog-football/
它在这里给出空指针
Element featureList = doc.select("div.callout-box").last();
featuresText.setText("FEATURE: " + featureList.text());
这是为什么?我正在尝试检索概述部分。它适用于所有其他项目。
When trying to parse the link
http://pc.gamespy.com/pc/bastion/
using
Element overview = doc.select("div#object-overview").last();
Element paragraph = overview.select("p").last();
It gives me a nullpointerexception.
And also with this one
http://wii.gamespy.com/wii/jerry-rice-nitus-dog-football/
it gives null pointer here
Element featureList = doc.select("div.callout-box").last();
featuresText.setText("FEATURE: " + featureList.text());
Why is this? I am trying to retrieve the overview section. it works for all the other items.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第一个上,您应该能够简单地调用
您不需要将 div 作为其中的一部分,因为
object-overview
是一个id
。您收到 NullPointerException 是因为 select 中的表达式不正确,因此select
返回 null,因为它找不到任何内容。不知道为什么第二个不适合你。我可以看到至少有一个带有类标注框的 div。除非
featuresText
为空?On the first one you should be able to simply call
You shouldn't need the div as part of it since
object-overview
is anid
. You were getting the NullPointerException because the expression in your select was incorrect, soselect
returned null because it couldn't find anything.Not sure why the second one wouldn't work for you. I can see there is at least one div with the class callout-box. Unless
featuresText
is null?根据 http://jsoup.org/apidocs/,Jsoup 抛出
NullPointerException
if该参数为空。换句话说,.select("div#object-overview")
返回 null 或.select("p")
。尝试首先检查 null,然后使用.last()
方法,如下所示
According to http://jsoup.org/apidocs/, Jsoup throws
NullPointerException
if the argument is null. In other words rather.select("div#object-overview")
returns null or.select("p")
. Try to check for null first, then use.last()
method like thisetc