在自定义数组中存储元素( Casting 帮助)
Storing elements inside a custom array ( Casting help)
我解析我的 XML(根标签 -- “Registry”)并存储在一个 Registry 类型的对象中。
例如:
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
你永远不会初始化数组 xmlentries
。
Registry[] xmlentries = null;
因此,当您尝试在此处为其分配对象时
xmlentries[i] = (Registry) jaxbUnmarshaller.unmarshal(files[i]);
它会抛出一个 NullPointerException
.
您应该使用 List
、Set
或初始化您的数组,如果您绝对想要一个数组
Registry[] xmlentries = new Registry[10];
(例如,对于包含 10 个元素的数组)。
或者,如果您想要一个文件大小的数组
[...]
File [] files = dir.listFiles(filter);
if (files != null) {
xmlentries = new Registry[files.length]; <-- intialization of your array here
for (int i = 0;i < files.length;i++) {
[...]
但是,我建议您使用 Set
(如果您想要唯一的条目)或 List
(如果您想要顺序),它比数组更容易操作。
Set<Registry> entries = new Hashet<>();
List<Registry> entries = new ArrayList<>();
我解析我的 XML(根标签 -- “Registry”)并存储在一个 Registry 类型的对象中。 例如: 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
你永远不会初始化数组 xmlentries
。
Registry[] xmlentries = null;
因此,当您尝试在此处为其分配对象时
xmlentries[i] = (Registry) jaxbUnmarshaller.unmarshal(files[i]);
它会抛出一个 NullPointerException
.
您应该使用 List
、Set
或初始化您的数组,如果您绝对想要一个数组
Registry[] xmlentries = new Registry[10];
(例如,对于包含 10 个元素的数组)。
或者,如果您想要一个文件大小的数组
[...]
File [] files = dir.listFiles(filter);
if (files != null) {
xmlentries = new Registry[files.length]; <-- intialization of your array here
for (int i = 0;i < files.length;i++) {
[...]
但是,我建议您使用 Set
(如果您想要唯一的条目)或 List
(如果您想要顺序),它比数组更容易操作。
Set<Registry> entries = new Hashet<>();
List<Registry> entries = new ArrayList<>();