开发者

Parsing a tag with JSOUP force closing for nullPointerException

开发者 https://www.devze.com 2023-03-29 02:25 出处:网络
When trying to parse the link http://pc.gamespy.com/pc/bastion/ using Element overview = doc.select(\"div#object-overview\").last();

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 t开发者_如何学运维his 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.


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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消