0
import java.util.*;

import java.io.*;

import java.io.Serializable;

class Employee implements Serializable{
    
    int empno;
    String ename;
    String contact;
    
    Employee(int empno, String ename, String contact){
        this.empno = empno;
        this.ename = ename;
        this.contact = contact;
    }
 
    public String toString() {
        return empno+" "+ename+" "+contact;
    }
}

public class Employees {

    public static void main(String[] args) throws Exception{
        
        int choice = -1;
        Scanner sc = new Scanner(System.in);
        Scanner sc1 = new Scanner(System.in);
        
        File file = new File("employee.txt");
        
        ArrayList<Employee> al = new ArrayList<Employee>();
        
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        
        if(file.isFile()) {
            ois = new ObjectInputStream(new FileInputStream(file));
            al = (ArrayList<Employee>)ois.readObject();
            ois.close();
        }
        
        do{
            System.out.println("1.INSERT");
            System.out.println("2.DISPLAY");
            System.out.println("0.EXIT");
            
            choice = sc.nextInt();
            
            switch(choice) {
            case 1:
                System.out.println("Enter how many Employees you want to add:");
                int n = sc.nextInt();
                for(int i = 0; i < n; i++) {
                System.out.println("Enter Employee No: ");
                int empno = sc.nextInt();
                
                System.out.println("Enter Employee Name: ");
                String name = sc1.nextLine();
                
                System.out.println("Enter Employee Contact: ");
                String contact = sc1.nextLine();
                
                al.add(new Employee(empno, name, contact));
                }
                oos = new ObjectOutputStream(new FileOutputStream(file));
                oos.writeObject(al);
                oos.close();
                break;
                
            case 2:
                System.out.println(al);
                break;
            }
        }while(choice!=0);
    }
}

I'm getting this exception:

Caused by: java.io.NotSerializableException: Employee
    at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1202)
    at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:362)
    at java.base/java.util.ArrayList.writeObject(ArrayList.java:866)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at java.base/java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1201)
    at java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1531)
    at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1453)
    at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1196)
    at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:362)
    at Employees.main(Employees.java:69)
Incredible
  • 3,495
  • 8
  • 49
  • 77
sushil
  • 17
  • 3
  • ObjectInputStream ois = null; and then assigned to ois = new ObjectInputStream(new FileInputStream(file)); ? Why ? – Incredible Oct 19 '22 at 10:00
  • Does this answer your question? [Why does writeObject throw java.io.NotSerializableException and how do I fix it?](https://stackoverflow.com/questions/13895867/why-does-writeobject-throw-java-io-notserializableexception-and-how-do-i-fix-it) – Turamarth Oct 19 '22 at 10:01
  • Do you have another class called `Serializable`? – Andy Turner Oct 19 '22 at 10:08
  • 1
    Nothing wrong with your code itself. That's not a text file though – g00se Oct 19 '22 at 10:20
  • @AndyTurner No. – sushil Oct 19 '22 at 10:22
  • I am assuming the class Employee should be public in order for making it serializable. Option 1) Try moving Employee class to different file Option 2) include the main method also in Employee class itself, make it public and remove Employees class. – Gopinath Radhakrishnan Oct 19 '22 at 11:10
  • I just tried pasting this code into my IDE, running it and testing. Everything works fine for me, including adding employees, exiting, then running again and displaying from the saved file. I did not receive any exceptions. There must be something else wrong. Perhaps the compiled classes need to be deleted and recompiled. – Tim Moore Oct 22 '22 at 02:20

0 Answers0