1

公共类 TestValidatorSample {

public static void main(String aa[]) throws SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder parser = dbf.newDocumentBuilder();

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    //      /test_elements.xsd
    Schema schema = factory.newSchema(new StreamSource(TestValidatorSample.class.getResource(
            "/xsds/pakagename/test_elements.xsd").toString()));

    Validator validator = schema.newValidator();
    DOMSource domsrc = new DOMSource(parser.parse(new InputSource("test-example.xml")));
    try {
        validator.validate(domsrc);
        System.out.println("Validation successfull!!!");
        //                  System.out.println(xmlFile.getSystemId() + " is valid");
    } catch (SAXException e) {
        System.out.println("Validation not successfull!!!");
        //                  System.out.println(xmlFile.getSystemId() + " is NOT valid");
        System.out.println("Reason: " + e.getLocalizedMessage());
    }
}

}

第 47 行的线程“main”java.lang.NullPointerException 中的异常。

我在加载模式时遇到空指针(如上所述)异常。在上面的代码片段中,我试图加载一个模式,该模式将用于验证在我的应用程序中生成的 xml。

谁能帮我找出为什么我得到空指针异常?

4

0 回答 0