I'm just guessing, at this point, but...

createCopy() creates a copy of the Element (and children) and not attached
to a Document.

Any path that begins with "//" is an absolute path that must start
from a Document.  Your copy has no Document, and thus, tells me
the Document is 'null', which is then somewhat difficult to work
with.

Probably the simple answer, when working with a sub-tree of Elements
that aren't attached to a Document is to avoid using xpaths that
expect a Document to be available.   Try a relative XPath instead,
by prepending a dot.

        .//list

-bob


On Sat, 16 Mar 2002, Poon, Leon wrote:

> 
> I am trying to select a single node on a COPY of an element, but I am
> getting NullPointerException
> when I tried it. For example, I am getting the null pointer exception on
> node2 below. Is this a bug,
> or am I not using it properly? Right now, I am cloning the Document to get
> past this problem, but
> I think this is a very kludgy solution. Any suggestion is greatly welcome.
> 
> ============================================================================
> ===================
> String str =
> "<wine><prices><list>13.99</list><discounted>11.00</discounted></prices></wi
> ne>";
> SAXReader reader = new SAXReader();
> Document doc = reader.read(new StringReader(str));
> 
> Element element = doc.getRootElement();
> Element node = (Element) element.selectSingleNode("//list");
> System.out.println("node: "+node.asXML());
> 
> try {
>         Element elem2 = element.createCopy();
>         Element node2 = (Element) elem2.selectSingleNode("//list");
>         System.out.println("node2: "+node2.asXML());
> }
> catch (Exception e) {e.printStackTrace();}
> 
> Document doc2 = doc.clone();
> Element elem3 = doc2.getRootElement();
> Element node3 = (Element) elem3.selectSingleNode("//list");
> System.out.println("node3: "+node3.asXML());
> ============================================================================
> ===================
> 
> Leon
> 
> 
> 
> 
> 
> This message contains information which may be confidential and privileged.
> Unless you are the addressee  (or authorized to receive for the addressee),
> you may not use, copy or disclose to anyone the message or any information
> contained in the message.  If you have received the message in error, please
> advise the sender by reply e-mail, and delete or destroy the message. 
> 
> Thank you.
> 
> 
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user
> 


_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to