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

JAXP validation throws null pointer exception with StAXSource

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • jaxp_1.4
    • jaxp_1.4, 5.0
    • xml

        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>

              joehw Joe Wang
              joehw Joe Wang
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: