Jeet Parekh
Jeet Parekh

Reputation: 740

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

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?

Upvotes: 4

Views: 31478

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85789

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

Upvotes: 5

Related Questions