I am trying to seriliaze an Object having a reference to another Object as instance variable. I follow the common way, nevertheless I cannot retrive in the main() method the second Object (which is non Serializable). Here is my Code:
public class Car {
String type;
int speed;
public Car(String s, int v){
type = s;
speed = v;
}
}
public class Employee implements Serializable {
String name;
int Id;
transient Car car;
public Employee(String s, int i, Car c){
name = s;
Id = i;
car = c;
}
private void writeObject(ObjectOutputStream os){
try {
os.defaultWriteObject();
os.writeObject(car);
} catch (IOException e) {
e.printStackTrace();
}
}
private void readObject(ObjectInputStream is){
try {
is.defaultReadObject();
car = (Car)is.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the main method:
public static void main(String[] args) {
Car c = new Car("noType", 100);
Employee e = new Employee("Aris", 1, c);
try {
FileOutputStream fo = new FileOutputStream("save.txt");
ObjectOutputStream out = new ObjectOutputStream(fo);
out.writeObject(e);
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
FileInputStream fi = new FileInputStream("save.txt");
ObjectInputStream in = new ObjectInputStream(fi);
Employee emp = (Employee) in.readObject(); // Comment
System.out.println(emp.car.speed);
in.close();
}catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
On the line “Comment” in the second try block it throws a nullPointerException. It cannot save the Car object this way. What should I do to get over it? Ofcource I want necessarily the Car class to remain non-seriliazable. If prefer to save the instrance variables of the Car object and to recreate it by the aid of them and its constructor, how can I save (and retrieve) the String attribute?
UPDATE:
When trying to save only the int instance variable, it works. The relevant methods are this time:
private void writeObject(ObjectOutputStream os){
try {
os.defaultWriteObject();
os.writeInt(car.speed);
} catch (IOException e) {
e.printStackTrace();
}
}
private void readObject(ObjectInputStream is){
try {
is.defaultReadObject();
car = new Car("theNew", is.readInt());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
But my question is still how (or if) I can save the entire object. After all I did not manage to save and retrieve the String. WriteString(), ReadString() methods
do not exist.
The line
is wrong.
Car is not Serializable.
Solutions:
make car serializable, and remove the line
Use a MyCar object which is Seriazable only for the purpose of ser/ deser, and create car from MyCar, see point 3.
create a new Car : car = new Car(); but where to get the data from? (see point 2)
seialize the fields of car, one by one using os.writeObject() and for primitives e.g os.writeInt();
use your own custom serialization, using DataOutputStream
Update answer to your update question
String is an object, you serialize it with os.writeObject(car.type);
See also ObjectOutputStream
Code using solution 4: