【问题标题】:Storing elements inside a custom array ( Casting help)将元素存储在自定义数组中(投射帮助)
【发布时间】: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


【解决方案1】:

你永远不会初始化你的数组xmlentries

Registry[] xmlentries = null;

所以当你尝试在这里给它分配一个对象时

xmlentries[i] = (Registry) jaxbUnmarshaller.unmarshal(files[i]);

它会抛出一个NullPointerException

如果您绝对想要一个数组,您应该使用ListSet 或初始化您的数组

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<>();

【讨论】:

  • 谢谢皮尔波。我使用了该列表并使其正常工作
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
  • 2010-11-22
  • 2017-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多