hi,

I am getting some null pointer exceptions for some xpath queries executed
on Axiom objects. Here is a sample code to reproduce the issue.

 OMFactory omFactory = OMAbstractFactory.getOMFactory();
        OMElement wrapperElement = omFactory.createOMElement(new QName("
http://test.com";, "wrapper"));

        PlaceOrder placeOrder = new PlaceOrder();
        placeOrder.setPrice(23.45);
        placeOrder.setQuantity(23);
        placeOrder.setSymbol("IBM");
        QName elementQName = new QName("http://test.com";, "placeOrder");

        XMLStreamReader xmlStreamReader =
                BeanUtil.getPullParser(placeOrder, elementQName, null,
true, false);
        StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(new
StreamWrapper(xmlStreamReader));

        OMElement omElement = stAXOMBuilder.getDocumentElement();
        wrapperElement.addChild(omElement);

        try {
            AXIOMXPath axiomxPath = new
AXIOMXPath("//sn:wrapper/sn:placeOrder");
            axiomxPath.addNamespace("sn", "http://test.com";);

            OMElement resultOMElement = (OMElement)
axiomxPath.selectSingleNode(wrapperElement);

            System.out.println("OMElement ==> " + resultOMElement);
        } catch (JaxenException e) {
            e.printStackTrace();
        }

Here the result is null. Even I build the wrapperElement before executing
the xpath it won't work.

But if I insert the following lines before xpath execution it works fine.

String resultString = wrapperElement.toString();
            StAXOMBuilder resultOMBuilder = new StAXOMBuilder(new
ByteArrayInputStream(resultString.getBytes()));
            wrapperElement = resultOMBuilder.getDocumentElement();

This means xpath and other things should be ok. And also this works fine
for the xpaths like

//sn:placeOrder/sn:price

what could be the reason?

thanks,
Amila.

-- 
Amila Suriarachchi
WSO2 Inc.
blog: http://amilachinthaka.blogspot.com/

Reply via email to