Parsing a tag with JSOUP force closing for nullPointerException

你离开我真会死。 提交于 2020-02-08 02:35:44

问题


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.


回答1:


On the first one you should be able to simply call

Element overview = doc.select("#object-overview").last();

You shouldn't need the div as part of it since object-overview is an id. You were getting the NullPointerException because the expression in your select was incorrect, so select 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?




回答2:


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 this

Elements overviews = doc.select("div#object-overview");
if(!overview==null){
Element overview = overviews.last();
}

etc



来源:https://stackoverflow.com/questions/7086934/parsing-a-tag-with-jsoup-force-closing-for-nullpointerexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!