JDK-6211561 : XPath.evaluate(String,Object,QName) throws exception if context node is null
  • Type: Bug
  • Component: xml
  • Sub-Component: jaxp
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-12-22
  • Updated: 2017-05-16
  • Resolved: 2016-08-17
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b133Fixed
Related Reports
Relates :  
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

Comments
It looks like it might have been changed in the final version of the specification. I agree with the change made in JDK-4991857 in that it's appropriate to report an error if item is null and the expression refers to the context, an Exception should be thrown. Clarification of the specification is needed.
15-06-2016