2011-08-17 34 views
0

當試圖解析使用解析與JSOUP力標記爲關閉NullPointerException異常

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

它給了我一個NullPointerException鏈接

http://pc.gamespy.com/pc/bastion/

而且也與此一

http://wii.gamespy.com/wii/jerry-rice-nitus-dog-football/

它給空指針這裏

Element featureList = doc.select("div.callout-box").last(); 
featuresText.setText("FEATURE: " + featureList.text()); 

這是爲什麼?我正在嘗試檢索概覽部分。它適用於所有其他項目。

回答

0

根據http://jsoup.org/apidocs/,如果參數爲空,則Jsoup拋出NullPointerException。換句話說,.select("div#object-overview")返回null或.select("p")。儘量先檢查空,然後使用.last()方法這樣

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

0

放在了第一位,你應該能夠簡單地調用

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

你不應該需要在div因爲object-overviewid。你得到NullPointerException,因爲你選擇的表達式不正確,所以select返回null,因爲它找不到任何東西。

不知道爲什麼第二個不會爲你工作。我可以看到至少有一個div與類標註框。除非featuresText爲空?