Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复';NotSerializableException:java.time.format.DateTimeFormatter';错误_Java_Class_Serialization_Notserializableexception - Fatal编程技术网

如何修复';NotSerializableException:java.time.format.DateTimeFormatter';错误

如何修复';NotSerializableException:java.time.format.DateTimeFormatter';错误,java,class,serialization,notserializableexception,Java,Class,Serialization,Notserializableexception,我正在尝试使用ObjectOutputStream将Arraylist中的所有对象保存到文件。对象的一个属性是LocalDate,每当我尝试写入文件时,都会返回一个错误NotSerializableException:java.time.format.DateTimeFormatter,尽管没有任何LocalDate的DateTimeFormatter 完全错误: java.io.NotSerializableException: java.time.format.DateTimeFormatt

我正在尝试使用ObjectOutputStream将Arraylist中的所有对象保存到文件。对象的一个属性是LocalDate,每当我尝试写入文件时,都会返回一个错误NotSerializableException:java.time.format.DateTimeFormatter,尽管没有任何LocalDate的DateTimeFormatter

完全错误:

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)
使用对象输出流的方法

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();
        }
    }
试图保存到文件的对象的示例

rents.add(new Rent(0001, "John Smith", true, "Roubaix Sport", LocalDate.of(2019, 03, 06), LocalDate.of(2019, 04, 05), 30, true));
对象类

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;
    }

这里没有太多的内容,但很明显,当您编写对象时,您正在尝试编写DateTimeFormatter。这让我相信Customer中定义了一个,但由于DTF没有实现Serializable,它就爆炸了


最好的解决方案是编辑Customer类以正确序列化。另一种选择是将其填充到您的Rent类中,但如果字段是私有的,则这可能是不可能的。

在检查Customer类时,我意识到我有一个未使用的DateTimeFormatter,我无意中复制粘贴了它而没有意识到。去掉这个问题就解决了