Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6211561

XPath.evaluate(String,Object,QName) throws exception if context node is null

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b133
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      FULL PRODUCT VERSION :
      java version "1.5.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
      Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      XPath.evaluate (String expression, Object item, QName returnType) throws unclear XPathExpressionException if context node is null and expression refers, for example, to some nodes, i.e. requires some context node to be evaluated.

      See the attached program:
      - first evaluation (the real usage of null context parameter) evaluates ok
      - second one evaluates to null, but without exceptions
      - third one generates exception

      Note that expression is a legal construct, and XPathExpressionException should be thrown in case the expression is incorrect.

      According to JavaDoc: "If a null value is provided for item, an empty document will be used for the context.". However the behavior is different when an empty Document object is passed (second evaluation) and when null is passed to the method (third evaluation).

      null can be passed by accident, this is basically a program's error, but it's really hard to understand from the stack trace what went wrong.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached test app: java test


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
        Program runs without exceptions.
      ACTUAL -
      The following exception is thrown:
      Exception in thread "main"
      javax.xml.transform.TransformerException: Unknown error in XPath.
      at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:296)
      at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:213)
      at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)
      at test.main(test.java:16)
      Caused by: java.lang.NullPointerException
      at com.sun.org.apache.xpath.internal.axes.AxesWalker.setRoot(AxesWalker.java:218)
      at com.sun.org.apache.xpath.internal.axes.WalkingIterator.setRoot(WalkingIterator.java:153)
      at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:212)
      at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:211)
      at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:268)
      ... 3 more
      ---------
      java.lang.NullPointerException
      at com.sun.org.apache.xpath.internal.axes.AxesWalker.setRoot(AxesWalker.java:218)
      at com.sun.org.apache.xpath.internal.axes.WalkingIterator.setRoot(WalkingIterator.java:153)
      at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:212)
      at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:211)
      at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:268)
      at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:213)
      at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)
      at test.main(test.java:16)
      --------------- linked to ------------------
      javax.xml.xpath.XPathExpressionException
      at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:289)
      at test.main(test.java:16)
      Caused by: javax.xml.transform.TransformerException: Unknown error in XPath.
      at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:296)
      at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:213)
      at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)
      ... 1 more
      Caused by: java.lang.NullPointerException
      at com.sun.org.apache.xpath.internal.axes.AxesWalker.setRoot(AxesWalker.java:218)
      at com.sun.org.apache.xpath.internal.axes.WalkingIterator.setRoot(WalkingIterator.java:153)
      at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:212)
      at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:211)
      at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:268)
      ... 3 more


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.xpath.XPath;
      import javax.xml.xpath.XPathConstants;
      import javax.xml.xpath.XPathFactory;
      import org.w3c.dom.Node;


      class test
      {
      public static void main (String[] args)
      throws Exception
      {
      XPath xPath = XPathFactory.newInstance().newXPath();
      xPath.evaluate ("1+1", (Node) null, XPathConstants.STRING);
      xPath.evaluate ("/node", DocumentBuilderFactory.newInstance ().newDocumentBuilder ().newDocument (), XPathConstants.STRING);
      xPath.evaluate ("/node", (Node) null, XPathConstants.STRING);
      }
      }
      ---------- END SOURCE ----------
      ###@###.### 2004-12-22 02:08:37 GMT

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              joehw Joe Wang
              Reporter:
              gmanwanisunw Girish Manwani (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: