javaexceptionobjectinputstreamclassnotfound

Unhandled ClassNotFound Exception when reading object


So, I'm trying to read in an Object from a file, and I can't figure out why I'm getting this exception, or how to fix it. Maybe you guys can help me out? I've tried messing around with the way I read the object, but can't get it quite right. Here is my code I'm getting the error on the line that reads listOfEmployeesIn[i] = (Employee) objIn.readObject();:

import java.util.Random;
import java.io.*;

public class ProjectFive{
    public static void main(String[] args) throws IOException{
        Random rn = new Random();
        RandomAccessFile file = new RandomAccessFile("employees.txt", "rw");
        FileOutputStream fileOut = new FileOutputStream("employees.txt");
        ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
        FileInputStream fileIn = new FileInputStream("employees.txt");
        ObjectInputStream objIn = new ObjectInputStream(fileIn);
        Object x;

        long SSN;
        float salary;
        int age;
        float maxSalary = 200000;
        float minSalary = 20000;
        long SSNRange = 1000000000;

        String[] names = {"Matty Villa"};
        Employee[] listOfEmployeesOut = new Employee[20];
        Employee[] listOfEmployeesIn = new Employee[20];

        for(int i=0;i<listOfEmployeesOut.length;i++){
            SSN = (long)(rn.nextDouble()*SSNRange);
            salary = rn.nextFloat()*(maxSalary - minSalary)+minSalary;
            age = rn.nextInt(57)+18;
            listOfEmployeesOut[i] = new Employee(SSN, names[i], salary, age);
        }
        for(int i = 0;i<listOfEmployeesOut.length;i++){
            objOut.writeObject(listOfEmployeesOut[i]);
        }
        for(int i = 0;i<listOfEmployeesIn.length;i++){
            listOfEmployeesIn[i] = (Employee) objIn.readObject();
        }
        file.close();
        fileOut.close();
        objOut.close();
        fileIn.close();
        objIn.close();


    }
}   
class Employee implements Serializable{
    public long socialSecurityNumber;
    public String fullName;
    public float salary;
    public int age;

    public Employee(long socialSecurityNumber, String fullName, float salary, int age){
        this.socialSecurityNumber = socialSecurityNumber;
        if(fullName.length() != 50){
            fullName = resizeString(fullName);
        }
        this.fullName = fullName;
        this.salary = salary;
        this.age = age;
    }

    private String resizeString(String s){
        if(s.length() < 50){
            for(int i = s.length(); i<=50; i++){
                s += ' ';
            }
        }else{
            s = s.substring(0,50);
        }
        return s;
    }
    public String toString(){
        String out = "Name: " + fullName + "\nSalary: " + salary + "\nSocial: " + socialSecurityNumber
            + "\nAge: " + age;
        return out;
    }
}

Solution

  • As per JAVA API for ObjectInputStream, the method readObject throws checked exceptions - IOException, ClassNotFoundException

    So either throw this exception from main method:

    public static void main(String[] args) throws IOException, ClassNotFoundException
    

    or handle it using try/catch blocks:

            try {
                listOfEmployeesIn[i] = (Employee) objIn.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }