0

When i try tto copy a store i get a few errors. The errors are:

 choice:java.io.NotSerializableException: Employee
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at java.util.HashMap.writeObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeWriteObject(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.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 FileUtility.save(FileUtility.java:27)
    at MainApp.start(MainApp.java:190)
    at MainApp.main(MainApp.java:16)

Here is also my code: MainApp

//---------------------------------------------------------------------------------------
//  Name:        Case 7: Store.
//  Description: Choice 7 gives the user an option to copy and read a store
//               using read and write class from Java.
//---------------------------------------------------------------------------------------
            case 7:
                System.out.println("Store");
                EmployeeStore copyMyStore = Store.copy();
                System.out.println(copyMyStore); //print contents to check equality
                //shallow copy would look like this...
                EmployeeStore shallowCopyMyStore;
                //both variables point to same object
                shallowCopyMyStore = Store; 
                //lets store and re-load the mystore object
                FileUtility.save("myStore.store", Store);
                /*EmployeeStore loadedStore 
                            = (EmployeeStore)FileUtility.load("myStore.nmcg");
                System.out.println("\n--- RELOADED STORE CONTENTS ---");
                loadedStore.print();
                //System.out.println("\n(static) Count: "  + EmployeeStore.print);*/


                break;

File Utility

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/**
  * <i>Use this class to save and load any type of object (i.e. a container class (e.g. PersonStore), an account, a task, a person)
 *   @author     NMCG
 *   @version    1.0            
*/
public class FileUtility /*serialization - step 2*/
{

    /**
     * @param fileName relative or absolute file path (e.g. "name.txt" or "c:\\temp\\name.txt"
     * @param obj      address of any object to be stored (i.e. a container class (e.g. PersonStore), an account, a task, a person)
     */

    public static void save(String fileName, Object obj)
    {
        try
        {
            FileOutputStream fos = new FileOutputStream(fileName);
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(obj);
            oos.flush();
            oos.close();
            fos.close();
        }
        catch(Exception e)
        {
            System.out.println("Something bad happened during the save phase!");
            e.printStackTrace();
        }
    }

    /**
     * @param  fileName relative or absolute file path (e.g. "name.txt" or "c:\\temp\\name.txt")
     * @return Object   address of the object loaded into RAM from file
     */

    public static Object load(String fileName)  // "test.txt"
    {
        Object objectIn = null;
        try
        {
            FileInputStream fis = new FileInputStream(fileName);
            ObjectInputStream ois = new ObjectInputStream(fis);

            objectIn = ois.readObject();

            ois.close();
            fis.close();
        }
        catch(Exception e)
        {
            System.out.println("Something bad happened during the save phase!");
            e.printStackTrace();
        }
        return objectIn;
    }
}

copy

// ---------------------------------------------------------------------------------------
// Name: Store copy.
// ---------------------------------------------------------------------------------------
public EmployeeStore copy()
{
    // instanciate the destination copy and give same name
    EmployeeStore Copy = new EmployeeStore();

    // by specifying the type of the entry in the for loop i.e. <>
    // we don't have to typecast the getKey() and getValue() methods
    // as shown in the commented out line inside the for loop
    for (Map.Entry<String, Employee> entry : map.entrySet()) 
    {
        // getting each key-value and putting into the copy
        // theCopy.add((MovieKey)entry.getKey(), (Movie)entry.getValue());
        Copy.add(entry.getValue());
    }
    // return address of the new MovieStore object
    return Copy;
}
// ---------------------------------------------------------------------------------------
Pendo826
  • 1,002
  • 18
  • 47
  • 1
    possible duplicate of [What causes a NotSerializableException?](http://stackoverflow.com/questions/6893833/what-causes-a-notserializableexception) – Dave Costa Jul 27 '12 at 17:16
  • What is your questions? Do you want to know why you didn't make Employee implement Serializable? – Peter Lawrey Jul 27 '12 at 17:21

3 Answers3

2

The error message says it all: the Employee class is not serializable. It doesn't implement the interface java.io.Serializable.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

Look up NotSerializableException. As its name suggests, it is thrown when an object is expected to implement the Serializable interface but does not. The exception report even tells you which class it is that needs to implement the interface, Employee in your case.

Dave Costa
  • 47,262
  • 8
  • 56
  • 72
1

choice:java.io.NotSerializableException: Employee

Your Employee class should implement Serializable interface. The code you posted is somewhat irrelevant and useless...

Shark
  • 6,513
  • 3
  • 28
  • 50