I'm creating a generic type ArrayQueue and I'm trying to save an ArrayQueue into a text file.
My code:
public void saveToFile(ArrayQueueInterface<Payment> paymentQ){
try {
File file = new File("payment.dat");
ObjectOutputStream ooStream = new ObjectOutputStream(new FileOutputStream(file));
ooStream.writeObject(paymentQ);
ooStream.close();
//this.dispose();
} catch (FileNotFoundException ex) {
System.out.println("ERROR : File not found");
} catch (IOException ex) {
System.out.println("ERROR : Cannot save to file");
}
}
The output showed was: "ERROR: Cannot save to file"
Does anyone have any idea how to fix this?
Here's the full stack trace :
at client.TestPayment.main(TestPayment.java:844)
java.io.NotSerializableException: adt.ArrayQueue
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at entity.PaymentOperation.saveToFile(PaymentOperation.java:337)
at entity.PaymentOperation.addPayment(PaymentOperation.java:72)
at client.TestPayment.init(TestPayment.java:545)
at client.TestPayment.main(TestPayment.java:844)
java.io.NotSerializableException: adt.ArrayQueue
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
In order to serialize
ArrayQueueInterface<Payment>successfully, the implementation of yourArrayQueueInterfaceshould implementSerializableinterface (judging by the information in the stack-trace, it's not the case).The same applies to the
Paymentclass.Note:
The whole object graph should be serializable, i.e. all the fields of
Payment(and if some of the fields are custom type, then their field as well) should implementSerializable.All primitive type are serializable, and all the standard classes from the JDK (apart from
ObjectandOptional). So you need to care primary about your custom classes.There's caveat regarding desirialization: