0

I have been playing around with serialization-XML in java and am a little stuck. When I run this program I get two exceptions and I am not sure what the cause is:

java.lang.InstantiationException: Ship
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(Ship);
Continuing ...

I suspect that there is something wrong with the class that I am trying to serialize because when I use an example of the internet it works fine.

Can someone please point out what mistake I am making.

Main:

public class Main {

    private static final String XMLLocation = "xmlTest.xml";
    static ObjectSerializationToXML serializer = new ObjectSerializationToXML();

    public Main() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Ship ship = new Ship("name", "324");
        serializer.serializeObjectToXML(XMLLocation, ship);

    }

}

Object Serialization-XML Class:

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ObjectSerializationToXML {

    /**
     * <span id="IL_AD10" class="IL_AD">This method</span> saves (serializes) any java bean object into xml file
     */
    public void serializeObjectToXML(String xmlFileLocation,
            Object objectToSerialize) throws Exception {
        FileOutputStream os = new FileOutputStream(xmlFileLocation);
        XMLEncoder encoder = new XMLEncoder(os);
        encoder.writeObject(objectToSerialize);
        encoder.close();
    }

    /**
     * Reads Java Bean Object From XML File
     */
    public Object deserializeXMLToObject(String xmlFileLocation)
            throws Exception {
        FileInputStream os = new FileInputStream(xmlFileLocation);
        XMLDecoder decoder = new XMLDecoder(os);
        Object deSerializedObject = decoder.readObject();
        decoder.close();

        return deSerializedObject;
    }
}

Object To Serialize (My object that causes the exception):

public class Ship {

    private String name;
    private String yearBuilt;


    public Ship(String name, String yearBuilt) {
        this.name = name;
        this.yearBuilt = yearBuilt;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getYearBuild() {
        return yearBuilt;
    }
    public void setYearBuild(String yearBuild) {
        this.yearBuilt = yearBuild;
    }
    @Override
    public String toString() {
        return "ship [name=" + name + ", yearBuilt=" + yearBuilt + "]";
    }

}

Object To Serialize (example from the internet that works):

public class MyBeanToSerialize {
    private String firstName;
    private String lastName;
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}
Zzz
  • 2,927
  • 5
  • 36
  • 58
  • 2
    The problem with what I was doing is that I needed a default constructor and to implement java.io.Serializable. These two steps would have made my class JavaBeans compliant and everything worked smoothly! – Zzz Apr 14 '13 at 23:29
  • public class Ship implements java.io.Serializable{ private String name; private String yearBuilt; public Ship(){ this.name = ""; this.yearBuilt = "0000"; } //Code } } – Zzz Apr 14 '13 at 23:30
  • Good job! Now please try to create an answer like and mark it, it may help others. :-) – porfiriopartida Sep 09 '13 at 20:30

1 Answers1

0

Whenever any class containing parameterized constructor and trying to serialize, then it should be only instantiated by default Constructor. So, XMLEncoder requires an object to serialize it by default constructor.

Ship class must implement the default constructor while it is containing parameterized constructor because whenever Ship class becomes to serializable, it would be looking for default constructor to instantiate for XMLEncoder.

Please find corrected Ship class as per below.

public class Ship {

private String name;
private String yearBuilt;


public Ship(String name, String yearBuilt) {
    this.name = name;
    this.yearBuilt = yearBuilt;
}

//Default constructor must be implemented for XMLEncoder serializing
public Ship() {
    super();
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
public String getYearBuild() {
    return yearBuilt;
}
public void setYearBuild(String yearBuild) {
    this.yearBuilt = yearBuild;
}
@Override
public String toString() {
    return "ship [name=" + name + ", yearBuilt=" + yearBuilt + "]";
}

}

Abhishek Shah
  • 1,394
  • 1
  • 16
  • 25