OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: [xml-dev] Java NPE at node.getAttribute( )

[ Lists Home | Date Index | Thread Index ]

You can only get an NPE in this case if the nodeList is empty (so item(0)
returns null).  If your document uses namespaces, you may need to have
prefixes on the steps of the path, e.g. "/p:applet/p:transport".  The
default namespace will *not* be used in an XPath.  A tag mentioned without a
prefix in an xpath expression will be assumed to be in the null namespace.
The prefixes in the path must either match those of xmlDocument or you need
to supply some context that provides the prefix -> namespace mapping.

Also, if you are using namespaces, you should be calling getAttributeNS
rather than getAttribute.

Jeff


----- Original Message ----- 
From: "Nishi Prafull" <nishiprafull@gmail.com>
To: "John Rivett-Carnac" <jbrc@blueyonder.co.uk>
Cc: <xml-dev@lists.xml.org>
Sent: Monday, January 24, 2005 3:06 PM
Subject: Re: [xml-dev] Java NPE at node.getAttribute( )


> Hi John:
> I tried your suggestion:
>  NodeList nodeList=xmlDocument.selectNodes("/applet/transport", nsr);
>   XMLElement  node=(XMLElement)nodeList.item(0);
>   String trans_val1 = node.getAttribute("name");
>
> But I still get a NPE at node.getAttribute("name").
>
> Can you suggest anything?
>
> Thanks.
>
> On Mon, 24 Jan 2005 22:00:06 -0000, John Rivett-Carnac
> <jbrc@blueyonder.co.uk> wrote:
> >
> > I think you should get the "transport" nodes in a node collection, then
get
> > the name attribute of each transport node:
> >
> > NodeList nodeList=xmlDocument.selectNodes("/applet/transport", nsr);
> > XMLElement  node=(XMLElement)nodeList.item(0);
> > String trans_val1 = node.getAttribute("name");
> >
> >
>
> -----------------------------------------------------------------
> The xml-dev list is sponsored by XML.org <http://www.xml.org>, an
> initiative of OASIS <http://www.oasis-open.org>
>
> The list archives are at http://lists.xml.org/archives/xml-dev/
>
> To subscribe or unsubscribe from this list use the subscription
> manager: <http://www.oasis-open.org/mlmanage/index.php>
>





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS