I am trying to use ObjectOutputStream to save all objects in an Arraylist to file. An attribute of the object is a LocalDate and whenever I try to write to file an error NotSerializableException: java.time.format.DateTimeFormatter returns despite having no DateTimeFormatter for any of the LocalDates
Full Error:
java.io.NotSerializableException: java.time.format.DateTimeFormatter
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1185)
at java.base/java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1553)
at java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1510)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1433)
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1179)
at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
at java.base/java.util.ArrayList.writeObject(ArrayList.java:791)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1130)
at java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1497)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1433)
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1179)
at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
at BikeNow.saveRent(BikeNow.java:330)
at BikeNow.main(BikeNow.java:114)
The method using Object output Stream
public void saveRent() {
//Create file and object output stream
try {
FileOutputStream fos = new FileOutputStream("tmp.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//Write array to file
oos.writeObject(rents);
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
An Example of object trying to be saved to file
rents.add(new Rent(0001, "John Smith", true, "Roubaix Sport", LocalDate.of(2019, 03, 06), LocalDate.of(2019, 04, 05), 30, true));
The Object Class
import java.io.Serializable;
import java.time.LocalDate;
public class Rent extends Customer implements Serializable {
private LocalDate startDate;
private LocalDate endDate;
private int duration;
private boolean overdue;
public Rent(int customerID, String customerName, boolean renting, String bikeRented, LocalDate startDate, LocalDate endDate, int duration, boolean overdue) {
super(customerID, customerName, renting, bikeRented);
this.startDate = startDate;
this.endDate = endDate;
this.duration = duration;
this.overdue = overdue;
}
There's not much to go on here, but clearly you're trying to write a DateTimeFormatter when you write your object. This leads me to believe there is one defined in Customer, but since DTF doesn't implement Serializable, it explodes.
The best solution is to edit your Customer class to properly serialize. The other option is to stuff it into your Rent class, but that might not be possible if the fields are private.