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

XPathExpression.evaluate() methods throw XPathException instead of NPE

XMLWordPrintable

    • beta2
    • generic
    • generic
    • Verified



      Name: eaR10174 Date: 11/11/2003


        The following methods of the class javax.xml.xpath.XPathExpression
        
           public Object evaluate(InputSource source, QName returnType)
                      throws XPathException
           public Object evaluate(InputSource source)
                      throws XPathException
           public Object evaluate(URL documentURL, QName returnType)
                      throws XPathException

      throw XPathException instead of NullPointerException (see test.java below) in the
      following cases:

           a) source is null; returnType is not null (method evaluate(InputSource,QName))
           b) source is not null; returnType is null (method evaluate(InputSource,QName))
           c) source is null (method evaluate(InputSource))
           d) documentURl is null (method evaluate(URL,QName))

           
      According to the javadoc the methods should throw NullPointerException in such cases.

        The bug appears in jdk1.5.0beta-b26 and affects new JCK1.5 tests:
        
            api/javax_xml/xpath/XPathExpression/index.html#Evaluate[Evaluate038]
            api/javax_xml/xpath/XPathExpression/index.html#Evaluate[Evaluate039]
            api/javax_xml/xpath/XPathExpression/index.html#Evaluate[Evaluate043]
            api/javax_xml/xpath/XPathExpression/index.html#Evaluate[Evaluate048]

      ------------------------------------test.xml------------------------------
      <?xml version="1.0"?>
      <root/>
      ------------------------------------test.java-----------------------------
      import javax.xml.namespace.QName;
      import javax.xml.xpath.XPath;
      import javax.xml.xpath.XPathFactory;
      import javax.xml.xpath.XPathConstants;
      import javax.xml.xpath.XPathException;
      import javax.xml.xpath.XPathExpression;
      import org.xml.sax.InputSource;
      import java.net.URL;
      import java.io.File;

      public class test {

          public static void main (String[] args) {
              XPathExpression expr = null;
              try {
                  XPathFactory xpf = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL);
                  XPath xpath = xpf.newXPath();
                  expr = xpath.compile("123");
              } catch (XPathException e) {
                  System.err.println("Can not compile expression.");
                  e.printStackTrace();
                  System.exit(1);
              }
              
              try {
                  System.out.println("Invoke evaluate((InputSource)null,QName).");
                  expr.evaluate((InputSource)null, XPathConstants.STRING);
                  System.out.println("NPE not thrown.");
              } catch (NullPointerException e) {
                  System.out.println("OK: NPE");
              } catch (Exception e) {
                  e.printStackTrace();
                  System.err.println("");
              }

              try {
                  System.out.println("Invoke evaluate(InputSource, null).");
                  expr.evaluate(new InputSource("test.xml"), null);
                  System.out.println("NPE not thrown.");
              } catch (NullPointerException e) {
                  System.out.println("OK: NPE");
              } catch (Exception e) {
                  e.printStackTrace();
                  System.err.println("");
              }

              try {
                  System.out.println("Invoke evaluate((InputSource)null).");
                  expr.evaluate((InputSource)null);
                  System.out.println("NPE not thrown.");
              } catch (NullPointerException e) {
                  System.out.println("OK: NPE");
              } catch (Exception e) {
                  e.printStackTrace();
                  System.err.println("");
              }

              try {
                  System.out.println("Invoke evaluate(URL, null).");
                  expr.evaluate(new File("test.xml").toURL(), null);
                  System.out.println("NPE not thrown.");
              } catch (NullPointerException e) {
                  System.out.println("OK: NPE");
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
      --------------------------------------------------------------------------
      % java -showversion test
      java version "1.5.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b26)
      Java HotSpot(TM) Client VM (build 1.5.0-beta-b26, mixed mode)

      Invoke evaluate((InputSource)null,QName).
      javax.xml.xpath.XPathException: java.lang.IllegalArgumentException: InputSource cannot be
      null
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.ja
      va:205)
              at test.main(test.java:27)
      Caused by: java.lang.IllegalArgumentException: InputSource cannot be null
              at
      com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java
      :202)
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.ja
      va:201)
              ... 1 more

      Invoke evaluate(InputSource, null).
      javax.xml.xpath.XPathException: java.lang.NullPointerException
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.ja
      va:205)
              at test.main(test.java:38)
      Caused by: java.lang.NullPointerException
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.getResultAsType(XPathExpression
      Impl.java:252)
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.ja
      va:115)
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.ja
      va:202)
              ... 1 more

      Invoke evaluate((InputSource)null).
      javax.xml.xpath.XPathException: java.lang.IllegalArgumentException: InputSource cannot be
      null
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.ja
      va:205)
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.ja
      va:246)
              at test.main(test.java:49)
      Caused by: java.lang.IllegalArgumentException: InputSource cannot be null
              at
      com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java
      :202)
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.ja
      va:201)
              ... 2 more

      Invoke evaluate(URL, null).
      javax.xml.xpath.XPathException: java.lang.NullPointerException
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.ja
      va:205)
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.ja
      va:164)
              at test.main(test.java:60)
      Caused by: java.lang.NullPointerException
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.getResultAsType(XPathExpression
      Impl.java:252)
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.ja
      va:115)
              at
      com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.ja
      va:202)
              ... 2 more
      --------------------------------------------------------------------------

      ======================================================================

            rmandavasunw Ramesh Mandava (Inactive)
            evgsunw Evg Evg (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: