I keep getting java.io.NotSerializableException: java.io.ObjectOutputStream

走远了吗. 提交于 2020-01-13 17:03:47

问题


This is the code that I have been trying

import java.util.Scanner;
import java.io.*;

abstract class Account implements Serializable {
    protected String accountHolderName;
    protected long balance;

    protected ObjectOutputStream accData;

    Scanner input = new Scanner(System.in);
}

class Savings extends Account implements Serializable {

    Savings() throws IOException {
        System.out.print("enter your name: ");
        accountHolderName = input.nextLine();
        System.out.print("\n");
        System.out.print("enter your balance: ");
        balance = input.nextLong();
        accData = new ObjectOutputStream(new FileOutputStream(accountHolderName + ".bin"));
        accData.writeObject(this);
        accData.close();
    }
}

class Banking implements Serializable {
    public static void main(String args[]) throws IOException {
        Scanner input = new Scanner(System.in);
        Savings savobj = new Savings();
    }
}

and this is the exception I get

Exception in thread "main" java.io.NotSerializableException: java.io.ObjectOutputStream at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) at java.io.ObjectOutputStream.writeSerialData(Unknown Source) at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source) at Savings.(Banking.java:22) at Banking.main(Banking.java:30)

I also tried using savobj.accData.writeObj(savobj) from main(), but I still get the same exception. What should I do?


回答1:


Only primitives and classes that implement Serializable interface can be serialized. ObjectOutputStream doesn't implement this interface.

Quick solution: use the ObjectOutputStream in the narrowest possible scope, declare it in the method where it's being used, not as a field in the class. Do similar with other utility classes like Scanner.

abstract class Account implements Serializable {
    protected String accountHolderName;
    protected long balance;

    //protected ObjectOutputStream accData;

    //Scanner input = new Scanner(System.in);
}

class Savings extends Account implements Serializable {

    Savings() throws IOException {
        Scanner input = new Scanner(System.in);
        System.out.print("enter your name: ");
        accountHolderName = input.nextLine();
        System.out.print("\n");
        System.out.print("enter your balance: ");
        balance = input.nextLong();
        ObjectOutputStream accData = new ObjectOutputStream(new FileOutputStream(accountHolderName + ".bin"));
        accData.writeObject(this);
        accData.close();
    }
}

Another solution may be just marking these fields as transient so they won't be serialized/deserialized:

abstract class Account implements Serializable {
    protected String accountHolderName;
    protected long balance;

    protected transient ObjectOutputStream accData;

     transient Scanner input = new Scanner(System.in);
}


来源:https://stackoverflow.com/questions/29592425/i-keep-getting-java-io-notserializableexception-java-io-objectoutputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!