United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6708840 : JAXP validation throws null pointer exception with StAXSource

Details
Type:
Bug
Submit Date:
2008-05-30
Status:
Resolved
Updated Date:
2013-05-16
Project Name:
JDK
Resolved Date:
2008-07-02
Component:
xml
OS:
generic
Sub-Component:
javax.xml.validation
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
jaxp_1.4,5.0
Fixed Versions:
jaxp_1.4 (1.4)

Related Reports
Backport:
Backport:

Sub Tasks

Description
Refer to JAXP forum:
http://forums.java.net/jive/thread.jspa?threadID=41626&tstart=0

Am I doing something wrong? I am attempting to create javax.xml.transform.stax.StAXSource object and pass it to a schema validator object and then call validate() but I get the following stack trace output when I run it:

ERROR: ''
Exception in thread "main" java.lang.NullPointerException
at com.sun.org.apache.xerces.internal.jaxp.validation.StAXValidatorHelper.validate(StAXValidatorHelper.java:95)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:114)
at javax.xml.validation.Validator.validate(Validator.java:127)
at alex.test.StaxExample.methodA(StaxExample.java:43)
at alex.test.StaxExample.main(StaxExample.java:22)

The schema .xsd file as well as the data .xml file are found and are good. On paper by reading the API's this should work. . .but it doesn't appear that it actually does. Here is the sample code, and I know I am not the only person who has encountered this problem as I took this from another forum elsewhere. But the post was somewhat old and I found nothing as far as a solution or answer by googling around for a while.

<code>
package alex.test;

import java.io.File;

import javax.xml.XMLConstants;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

public class StaxExample {

public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.println("Usage : StaxExample <xmlFile> <schemaFile>");
} else {
methodA(args[0], args[1]);
}
}

private static void methodA(String xmlFile, String xsdFile)
throws Exception {
XMLInputFactory xmlif = XMLInputFactory.newInstance();

XMLStreamReader staxReader = xmlif.createXMLStreamReader(new StreamSource(xmlFile));

SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schemaGrammar = schemaFactory.newSchema(new File(xsdFile));

Validator schemaValidator = schemaGrammar.newValidator();

Source staxSrc = new StAXSource(staxReader);
schemaValidator.validate(staxSrc);

while (staxReader.hasNext()) {
int eventType = staxReader.next();
System.out.println("Event of type: " + eventType);
}
}
}
</code>

                                    

Comments
EVALUATION

Fix in JAXP 1.4. Will request an integration into a JDK6 update release.
                                     
2008-07-02



Hardware and Software, Engineered to Work Together