0

I have written the code for parsing image url from <image> tag.

The code is below:

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";
            }

It was working fine with the rss feed which having <image> tag. I want to set default image for the Rss which does not having <image> url.

But my code showing java.lang.NullPointerException in this line NodeList imageUrl = element.getElementsByTagName("url");.

How to check null for NodeList?

And give me any idea to rectify it.

Thank you in advance!!!

Dhasneem
  • 4,037
  • 4
  • 33
  • 47

1 Answers1

0

Surround yor code NodeList imageUrl = element.getElementsByTagName("url"); with try-catch and catch the exception if no `url Tag is found.

Like this

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

Hope it helps...

AnujMathur_07
  • 2,586
  • 2
  • 18
  • 25