使用JAXB返回null的解组

时间:2014-05-06 07:37:31

标签: xml

        Im getting nullpointer exception while trying to unmarshal, when i execute main.
        Y is that so ? I have represented all classes correctly annotated them. 

Here are my java files :

        UAAMetaDataReportingModel.java

        @XmlRootElement(name = "UAAMetaDataReportingModel")
        @XmlAccessorType(XmlAccessType.FIELD) 
        public class UAAMetaDataReportingModel {

            @XmlElement(name = "accountDN")
            private String accountDN;
            @XmlElementWrapper(name = "UAAMetaDataReportingModel")
            @XmlElement(name = "uaaOperations")
            private UAAOperationsModel[] uaaOperations;
            @XmlElement(name = "eventTimestamp")
            private Date eventTimestamp;
            @XmlElement(name = "changeType")
            private String changeType;
            @XmlElement(name = "changeTimestamp")
            private Date changeTimestamp;



            public UAAMetaDataReportingModel(String accountDN,
                    UAAOperationsModel[] uaaOperations, Date eventTimestamp,
                    String changeType, Date changeTimestamp) {
                super();
                this.accountDN = accountDN;
                this.uaaOperations = uaaOperations;
                this.eventTimestamp = eventTimestamp;
                this.changeType = changeType;
                this.changeTimestamp = changeTimestamp;
            }

            public UAAMetaDataReportingModel() {

            }

            public String getAccountDN() {
                return accountDN;
            }

            public void setAccountDN(String accountDN) {
                this.accountDN = accountDN;
            }

            public UAAOperationsModel[] getUaaOperations() {
                return uaaOperations;
            }

            public void setUaaOperations(UAAOperationsModel[] uaaOperations) {
                this.uaaOperations = uaaOperations;
            }

            public Date getEventTimestamp() {
                return eventTimestamp;
            }

            public void setEventTimestamp(Date date) {
                this.eventTimestamp = date;
            }

            public String getChangeType() {
                return changeType;
            }

            public void setChangeType(String changeType) {
                this.changeType = changeType;
            }

            public Date getChangeTimestamp() {
                return changeTimestamp;
            }

            public void setChangeTimestamp(Date changeTimestamp) {
                this.changeTimestamp = changeTimestamp;
            }

        }

UAAOperationsModel.java

        @XmlRootElement(name = "uaaOperations")
        @XmlAccessorType(XmlAccessType.FIELD) 
        public class UAAOperationsModel {

            @XmlElementWrapper(name = "uaaOperations")
            @XmlElement(name = "operations")
            private List<String> operations;

             @XmlElement(name = "vin")
            private String vin;

            public UAAOperationsModel(List<String> operations, String vin) {
                super();
                this.operations = operations;
                this.vin = vin;
            }

            public UAAOperationsModel() {

            }

            public List<String> getOperations() {
                return operations;
            }
            public void setOperations(List<String> operations) {
                this.operations = operations;
            }
            public String getVin() {
                return vin;
            }
            public void setVin(String vin) {
                this.vin = vin;
            }

        }

UAAMetaDataReportingModelList.java

        @XmlRootElement(name = "UAAMetaDataReportingModelList")
        @XmlAccessorType(XmlAccessType.FIELD) 
        public class UAAMetaDataReportingModelList {

            @XmlElementWrapper(name = "UAAMetaDataReportingModelList")
            @XmlElement(name = "UAAMetaDataReportingModel")
            private List<UAAMetaDataReportingModel> uaaMetaDataReportingModel;



            public UAAMetaDataReportingModelList() {

            }

            public List<UAAMetaDataReportingModel> getUAAMetaDataReportingModel() {
                return uaaMetaDataReportingModel;
            }

            public void setUAAMetaDataReportingModel(List<UAAMetaDataReportingModel> uAAMetaDataReportingModel) {
                this.uaaMetaDataReportingModel = uAAMetaDataReportingModel;
            }



        When i try to execute the code through main

        public static void main(String[] commandParameters) {

        try {
                                    fileName = "C:/Users/swetha.sree/Desktop/data.xml";
                                            File file = new File(fileName);
                                            JAXBContext jaxbContext = JAXBContext.newInstance(UAAMetaDataReportingModelList.class);

                                            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                                            UAAMetaDataReportingModelList list = (UAAMetaDataReportingModelList) jaxbUnmarshaller.unmarshal(file);
                                            System.out.println(list.getUAAMetaDataReportingModel().toString());
                                            }
                                } catch (JAXBException e) {
                                      System.out.println("There is some issue with File conversion:: "+ e.getMessage());
                                    e.printStackTrace();
                                  }catch (Exception e) {
                                    // TODO Auto-generated catch block
                                        e.printStackTrace();

                                }

        }

  this is my xml file :

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <UAAMetaDataReportingModelList>

    <UAAMetaDataReportingModel>
    <accountDN>test123acdn3@uconnect</accountDN>

    <uaaOperations>
    <operations>doorLock</operations>
    <operations>doorUnlock</operations>
    <vin>3GGPG6FVXAT770701</vin>
    </uaaOperations>

    <eventTimestamp>2014-05-01 12:19:26.003113</eventTimestamp>
    <changeType>CREATE</changeType>
    <changeTimestamp>2014-05-01 12:19:26.003113</changeTimestamp>

    </UAAMetaDataReportingModel>

    <UAAMetaDataReportingModel>
    <accountDN>test123acdn3@uconnect</accountDN>

    <uaaOperations>
    <operations>doorLock</operations>
    <operations>doorUnlock</operations>
    <vin>3GGPG6FVXAT770701</vin>
    </uaaOperations>

    <eventTimestamp>2014-05-01 12:19:26.003113</eventTimestamp>
    <changeType>CREATE</changeType>
    <changeTimestamp>2014-05-01 12:19:26.003113</changeTimestamp>

    </UAAMetaDataReportingModel>

    </UAAMetaDataReportingModelList>


        ***

我在尝试unmarshal时遇到nullpointer异常,当我执行main时。是这样的吗?我代表所有类正确地注释了它们。我有一个uaaoperationslist,其中包含uaa操作列表,其中包含一些id和操作列表。


1 个答案:

答案 0 :(得分:0)

尝试省略

@XmlElementWrapper(name = "uaaOperations")

@XmlElementWrapper(name = "UAAMetaDataReportingModel")

@XmlElementWrapper(name = "UAAMetaDataReportingModel")

这产生了一个我可以导航的对象树。其余的取决于要求。