You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by Terry Rosenbaum <Te...@amicas.com> on 2002/08/08 18:56:58 UTC

NamedNodeMapImpl.removeNamedItemNS throws NullPointerException (versions 1.0 & 1.1)

There appears to be a bug in the
NamedNodeMapImpl.removeNamedItemNS
that causes a null pointer exception if
the Node has any elements in the deafult
namespace.

The contract's for both Node.getNamespaceURI() and
node.getLocalName() specify conditions under which
null is returned. For example, Node.getNamespaceURI()
returns null when the Node is in the default namespace
(a common condition).

Fix:

In NamedNodeMapImpl.java (org.apache.xindice.xml.dom.NamedNodeMapImpl):

Changed:

   public Node removeNamedItemNS(String namespaceURI, String localName) 
throws DOMException {
      Iterator enum = iterator();
      Node node;
      for ( int i = 0; enum.hasNext(); i++ ) {
         node = (Node)enum.next();
         if ( node.getNamespaceURI().equals(namespaceURI)
           && node.getLocalName().equals(localName) ) {
            return removeNamedItem(node.getNodeName());
         }
      }
      return null;
   }

To:

   public Node removeNamedItemNS(String namespaceURI, String localName) 
throws DOMException {
      Iterator enum = iterator();
      Node node;
      for ( int i = 0; enum.hasNext(); i++ ) {
         node = (Node)enum.next();
         if ( namespaceURI.equals(node.getNamespaceURI())
           && localName.equals(node.getLocalName()) ) {
            return removeNamedItem(node.getNodeName());
         }
      }
      return null;
   }

One possible workaround:

Use Node.getAttributes() to retrieve the list of attributes
and search the list yourself using Node.getNamespaceURI()
and Node.getLocalName() to check the nodes until
you find the desired node. Then use Node.getNodeName() to
get the node name and Node.removeNamedItem(String name)
to remove it.

-Terry