我有以下代码:
L2SchemaHandler handler = getSchemaHandler();
// validate
XMLFormatValidator validator = new XMLFormatValidator(handler.elements, handler.types, handler.root);
InputStream schemaInput = new FileInputStream(new File(xmlFilePath));
SAXParserFactory spfactory = SAXParserFactory.newInstance();
spfactory.setFeature("http://xml.org/sax/features/namespaces", true);
SAXParser parser = spfactory.newSAXParser();
//this line gives the null pointer Exception
parser.parse(schemaInput, validator);
report.addAll(validator.getReport());
this.objectsList = validator.getObjectList();
// When we know objects are valid, get duplicate objects from db
if(report.isEmpty())
duplicateObjectsMap = validateObjectID(validator.getObjects(),report);
// here we need to check whether object id's exists in db
if(report.isEmpty()){//if no errors have occurred
// if(validateObjectID(validator.getObjects(),report))
validateObjectsFromFile(validator.getObjects(), report , msgreport);
}
标记的行提供空指针异常,而parser
,schemaInput
和validator
包含的对象不是空值。
为什么这一行给出空指针异常?
答案 0 :(得分:0)
要使用验证解析器,请进行以下更改:
spfactory.setValidating(true);
默认情况下,this的值设置为false。