0

我已经编写了从标签解析图像 url 的<image>代码。

代码如下:

NodeList imageLink = docElement.getElementsByTagName("image");
            String nUrl;
            if (imageLink.toString() != null) {
                Element element = (Element) imageLink.item(0);
                NodeList imageUrl = element.getElementsByTagName("url");
                if (imageUrl.toString() != null) {
                    Element imageFirst = (Element) imageUrl.item(0);
                    nUrl = imageFirst.getFirstChild().getNodeValue();
                    Log.d(TAG,
                            "<<<<<<<<<<<<<<<<<<<<<<..............Image Url is : "
                                    + nUrl
                                    + ".....................>>>>>>>>>>>>>>>>>.....");
                } else {
                    Log.d(TAG,
                            "<<<<<<<<<<<<<<<<<<<<<<..............Image Url is null : .....................>>>>>>>>>>>>>>>>>.....");
                    nUrl = "http://static.dnaindia.com/images/710/logo_dna_rss.gif";
                }
            } else {
                Log.d(TAG,
                        "<<<<<<<<<<<<<<<<<<<<<<..............Image tag is not found.....................>>>>>>>>>>>>>>>>>.....");
                nUrl = "http://static.dnaindia.com/images/710/logo_dna_rss.gif";
            }

它与rss feed which having <image> tag. 我想为没有<image>url 的 Rss 设置默认图像。

但我的代码显示java.lang.NullPointerException在这一行NodeList imageUrl = element.getElementsByTagName("url");

如何检查 null NodeList

并给我任何纠正它的想法。

先感谢您!!!

4

1 回答 1

0

NodeList imageUrl = element.getElementsByTagName("url");用 try-catch包围你的代码,如果没有找到 `url 标记,则捕获异常。

像这样

try{
    NodeList imageUrl = element.getElementsByTagName("url");
} catch(NullPointerException e){
    nUrl = "http://static.dnaindia.com/images/710/logo_dna_rss.gif";
    e.printStackTrace();
}

希望能帮助到你...

于 2013-04-29T06:55:27.930 回答