【发布时间】:2021-11-04 01:11:24
【问题描述】:
我解析我的 XML(根标记 -- “注册表”)并存储在注册表类型的对象中。 例如: XML
<?xml version="1.0"?>
<Registry>
<Schools>
<School loc= "XXX">
<Student name = "XXX"/>
<Student name = "XX1"/>
<Student name = "XX2"/>
</School>
<School loc= "YYY">
<Student name = "XXX"/>
<Student name = "XY1"/>
<Student name = "XY2"/>
</School>
<School loc= "ZZZ">
<Student name = "YXX"/>
<Student name = "YX1"/>
<Student name = "YX2"/>
</School>
<School loc= "AAA">
<Student name = "ZXX"/>
<Student name = "ZX1"/>
<Student name = "ZX2"/>
</School>
</Schools>
</Registry>
XML 如下解组并存储在根标记类型的对象中。
File xmlFile = new File("studteachobjmodel.xml");
JAXBContext jaxbContext;
jaxbContext = JAXBContext.newInstance(Registry .class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Registry xmlentries = (Registry) jaxbUnmarshaller.unmarshal(xmlFile);
现在,如果我有一系列这样的 XML 文件,我声明一个 Registry[] xmlentries 类型的数组对象
我想将 XML 条目存储在 Registry 类型的数组中。我做了以下,但它显示一个错误
Registry[]xmlentries = null;
JAXBContext jaxbContext;
File dir = new File("XMLFiles");
if (dir.exists() && dir.isDirectory()) {
FileFilter filter = new FileFilter() {
@Override
public boolean accept(File file) {
return file.isFile() && file.getName().endsWith(".xml");
}
};
File [] files = dir.listFiles(filter);
if (files != null) {
for (int i =0;i <files.length;i++) {
jaxbContext = JAXBContext.newInstance(Registry.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
xmlentries[i] = (Registry) jaxbUnmarshaller.unmarshal(files[i]); //ERROR
}
}
}
错误:
Exception in thread "main" java.lang.NullPointerException: Cannot store to object array because "xmlentries" is null
at code.GenerateXML.main(GenerateXML.java:31)
请提供任何解决此问题的提示。 TIA
【问题讨论】:
-
第 31 行是哪一行?
-
图片中的错误行#
标签: java xml jaxb marshalling unmarshalling