You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by bu...@apache.org on 2002/12/10 09:23:17 UTC
DO NOT REPLY [Bug 15218] New: -
XPathApi.selectNodeList throws NullPointerException
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=15218>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND
INSERTED IN THE BUG DATABASE.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=15218
XPathApi.selectNodeList throws NullPointerException
Summary: XPathApi.selectNodeList throws NullPointerException
Product: XalanJ2
Version: 2.4
Platform: Other
OS/Version: Other
Status: NEW
Severity: Normal
Priority: Other
Component: org.apache.xml.dtm
AssignedTo: xalan-dev@xml.apache.org
ReportedBy: David.Ostrovsky@dvg.de
Since version xalan 2-4-0 (also in version 2-4-1) I'm getting a
NullPointerException:
Exception in thread "main" java.lang.NullPointerException
at org.apache.xml.dtm.ref.DTMNodeList.item(DTMNodeList.java:164)
at ApplyXPath2.doMain(ApplyXPath2.java:134)
at ApplyXPath2.main(ApplyXPath2.java:164)
Here is what I'm doing:
NodeList nl = XPathAPI.selectNodeList(doc, xpath);
int index = 0;
for (; nl.item(index) != null; index++);
With XPathAPI.slectNodeIterator(..) i've no problems.
Tested XML document (in distribution xalan-j_2_4_1/samples/ApplyXPath)
<?xml version="1.0"?>
<doc>
<name first="David" last="Marston"/>
<name first="David" last="Bertoni"/>
<name first="Donald" last="Leslie"/>
<name first="Emily" last="Farmer"/>
<name first="Joseph" last="Kesselman"/>
<name first="Myriam" last="Midy"/>
<name first="Paul" last="Dick"/>
<name first="Stephen" last="Auriemma"/>
<name first="Scott" last="Boag"/>
<name first="Shane" last="Curcuru"/>
</doc>
Source Code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xpath.XPathAPI;
import org.apache.xml.utils.TreeWalker;
import org.apache.xml.utils.DOMBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.SAXException;
import org.xml.sax.InputSource;
// Imported JAVA API for XML Parsing 1.0 classes
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
// Imported Serializer classes
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
/**
* Very basic utility for applying an XPath epxression to an xml file and
printing information
/ about the execution of the XPath object and the nodes it finds.
* Takes 2 arguments:
* (1) an xml filename
* (2) an XPath expression to apply to the file
* Examples:
* java ApplyXPath foo.xml /
* java ApplyXPath foo.xml /doc/name[1]/@last
* @see XPathAPI
*/
public class ApplyXPath2
{
protected String filename = null;
protected String xpath = null;
/** Process input args and execute the XPath. */
public void doMain(String[] args)
throws Exception
{
filename = args[0];
xpath = args[1];
if ((filename != null) && (filename.length() > 0)
&& (xpath != null) && (xpath.length() > 0))
{
// Tell that we're loading classes and parsing, so the time it
// takes to do this doesn't get confused with the time to do
// the actual query and serialization.
System.out.println("Loading classes, parsing "+filename+", and
setting up serializer");
// Set up a DOM tree to query.
InputSource in = new InputSource(new FileInputStream(filename));
DocumentBuilderFactory dfactory =
DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
Document doc = dfactory.newDocumentBuilder().parse(in);
// Set up an identity transformer to use as serializer.
Transformer serializer =
TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
"yes");
// Use the simple XPath API to select a nodeIterator.
System.out.println("Querying DOM using "+xpath);
NodeList nl = XPathAPI.selectNodeList(doc, xpath);
// Serialize the found nodes to System.out.
System.out.println("<output>");
int index = 0;
for (; nl.item(index) != null; index++);
System.out.println("Found "+index+" applicationentries");
System.out.println("</output>");
}
else
{
System.out.println("Bad input args: " + filename + ", " + xpath);
}
}
/** Decide if the node is text, and so must be handled specially */
static boolean isTextNode(Node n) {
if (n == null)
return false;
short nodeType = n.getNodeType();
return nodeType == Node.CDATA_SECTION_NODE || nodeType ==
Node.TEXT_NODE;
}
/** Main method to run from the command line. */
public static void main (String[] args)
throws Exception
{
if (args.length != 2)
{
System.out.println("java ApplyXPath filename.xml xpath\n"
+ "Reads filename.xml and applies the xpath; prints the nodelist
found.");
return;
}
ApplyXPath2 app = new ApplyXPath2();
app.doMain(args);
}
} // end of class ApplyXPath
Program call:
java ApplyXPath2 foo.xml /