Search code examples
javajaxbxml-serializationxml-deserialization

How to introduce a no argument constructor in case we have final field which are not initialized while using JAXB?


I am trying to use JAXB to serialize my java objects to XML. In order to use JAXB the class that has to be serialized must have a no argument constructor. But my problem is all the feilds are final so I cannot do this. If I set the class fields to null like

private final double Offset;
public MyNoArgumentConstructor()
   {
this.Offset = null; 
}

It would throw a null pointer exception . Any Idea on how I can achieve this?


Solution

  • If you declare as a Double object instead of double primitive you won't get an NPE with that code.