获取java.io.WriteAbortedException:写入已中止; java.io.NotSerializableException,即使我实现serializable

时间:2014-03-02 19:40:25

标签: java object serialization

public static void addToFile(LinkedList<Carowners> carowner)
{
    try
    {
    File file = new File(filename);
        FileOutputStream fout= new FileOutputStream(file);
        ObjectOutputStream out= new ObjectOutputStream(fout);
        out.writeObject(carowner);
        out.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

}

嗨,这是我的代码。当我进行对象序列化时,我遇到了以下错误。我为类实现了serializable。

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: 

Billing.Person
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at java.util.LinkedList.readObject(LinkedList.java:1136)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1004)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1872)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1777)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at Billing.Carownerslist.<init>(Carownerslist.java:42)
    at Billing.Carownerslist.main(Carownerslist.java:253)
Caused by: java.io.NotSerializableException: Billing.Person
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
    at java.util.LinkedList.writeObject(LinkedList.java:1118)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
    at Billing.Carownerslist.addToFile(Carownerslist.java:242)
    at Billing.Carownerslist.addCompany(Carownerslist.java:160)
    at Billing.Carownerslist.main(Carownerslist.java:285)
Exception in thread "main" java.lang.NullPointerException
    at Billing.Carownerslist.addCompany(Carownerslist.java:149)
    at Billing.Carownerslist.main(Carownerslist.java:278)
Java Result: 1

1 个答案:

答案 0 :(得分:-1)

例外是在流中。现在您已经创建了Serializable类。

,您必须重新创建流