简体   繁体   中英

DOM Parser thowing null pointer exception if XML has enclosure tag

I am creating an android app to parse an RSS feed and for the past few weeks it has been working well. But recently one of the posts had an <enclosure> tag and the app stopped working and the logcat stated that the DOMParser threw a NullPointerException.
I went through and tried everything my knowledge of java could possibly do. But to no avail.

As a side note if I manually remove the problematic tag, the problem is solved and the app works fine again.

This is the xml that I'm parsing

<item>
<title>Title</title>
<link>Link</link>
<pubDate>Sun, 27 Jan 2013 05:08:30 +0000</pubDate>
<dc:creator></dc:creator>
<guid isPermaLink="false">guid</guid>
<content:encoded>
Content
</content:encoded>
<enclosure url="" length="22836034" type="audio/mpeg"/>
</item>

I've removed the un-relevant tags and replaced the information for privacy and the sake of keeping it short

And a section of the parser:

Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();


            NodeList nl = doc.getElementsByTagName("item");



            int length = nl.getLength();

            for (int i = 0; i < length; i++) {
                Node currentNode = nl.item(i);
                RSSItem _item = new RSSItem();




                NodeList nchild = currentNode.getChildNodes();
                int clength = nchild.getLength();


                for (int j = 1; j < clength; j = j + 2) {

                    Node thisNode = nchild.item(j);
                    String nodeContent = null;
                    String nodeName = thisNode.getNodeName();


                    nodeContent = nchild.item(j).getFirstChild().getNodeValue();

In the logcat it tells me that this line:

nodeContent = nchild.item(j).getFirstChild().getNodeValue();

is the reason for the NullPointerException, but as I said earlier, if the feed doesn't have the <enclosure> tag it works like it should.

So to sum it up, if the XML file has an <enclosure> tag, it will cause the app to no longer work. But with out the tag, it works fine. What I am trying to do is to either ignore the tag or remove it (properly) and continue parsing the rest of the document.
At the moment nothing is working for me, which is why I am asking here.

Any help would be greatly appreciated.

Well yes - you're asking for the value of the first child of each of the child nodes of item .

The enclosure node doesn't have any child nodes, so getFirstChild returns null, as documented:

The first child of this node. If there is no such node, this returns null.

So it's fine to call getFirstChild() on any node, but you should then check whether the value is null before you call getNodeValue() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM