java - fail to marshal nested xml elements -


im trying create xml java class nested element. don't mistake , should change thanks!

my main class:

string my_xml = "my path...";  //hier path...  jaxbcontext context = jaxbcontext.newinstance(masterdatarm.class); marshaller m = context.createmarshaller(); m.setproperty(marshaller.jaxb_formatted_output, boolean.true);   master temp = new master();  temp.settransactionstatus("almostok");  m.marshal(temp, new file(my_xml));  m.marshal(temp, system.out); 

my class marshalled:

@xmlaccessortype(xmlaccesstype.field) @xmlrootelement public class master {      @xmlelement     protected date responsedatetime = new date();      @xmlelement     protected transaction transaction;       public void setresponsedatetime(date date){         this.responsedatetime = date;     }      public date getdate(){         return responsedatetime;     }      public static class transaction{         @xmlelement         string status = "ok";     }      public void setstatus(string status){          transaction.status = status; //  throws nullpointerexception !!!     }      public string getstatus(){         return transaction.status;     } 

transaction.status = status; // throws nullpointerexception !!! 

because not initializing inner static class, remember ways initialize instance variable before user , that's why getting nlp

create transcation object first , set using outer class object as

temp.settransaction(new master.transaction()); 

update class like

@xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class master {      public master() {     }      @xmlelement     protected date responsedatetime = new date();      @xmlelement     protected transaction transaction;      public void setresponsedatetime(date date) {         this.responsedatetime = date;     }      public date getdate() {         return responsedatetime;     }      public static class transaction {         @xmlelement         string status = "ok";     }      public void setstatus(string status) {         transaction.status = status; // throws nullpointerexception !!!     }      public string getstatus() {         return transaction.status;     }      public transaction gettransaction() {         return transaction;     }      public void settransaction(transaction transaction) {         this.transaction = transaction;     }  } 

marsh it

jaxbcontext jaxbcontext = jaxbcontext.newinstance(master.class);         marshaller jaxbmarshaller = jaxbcontext.createmarshaller();         jaxbmarshaller.setproperty(marshaller.jaxb_formatted_output, true);          master temp = new master();         temp.settransaction(new master.transaction());         temp.setstatus("almostok");         jaxbmarshaller.marshal(temp, new file(my_xml));         jaxbmarshaller.marshal(temp, system.out); 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -