Serializing a HashTable, Java
java, serialization
Solution
Even if the `Hashtable` object is serializable, the objects that you are storing inside it must be serializable as well. So I would first check to see if whatever you're storing inside your `Hashtable` implements the `Serializable` interface. At the very least, your `Store` class should also implement the `Serializable` interface.
UPDATE
Based on your updated question, it looks like the `Item` class will need to implement `Serializable` as well. In fact, this is exactly what the first line of the exception says:
java.io.NotSerializableException: Item
Problem
I have never used Serialization before. I think I have it all right except the last part in my "Q" case-switch. ``` public class Test{ public static void main(String args[]){ Store store = new Store(); FileOutputStream fos; ObjectOutputStream oos = null; try{ fos = new FileOutputStream(new File("table.obj")); oos = new ObjectOutputStream(fos); }catch(IOException e1){ e1.printStackTrace(); } ``` This goes on to contain a bunch of more code but what I think is really important is my "Q" case... ``` case "Q": System.out.println("Good-Bye!"); try{ oos.writeObject(store); oos.flush(); oos.close(); }catch(IOException e){ e.printStackTrace(); } System.exit(0); break; ``` When I attempt to save all the data to my .obj file and close the streams and exit my program I get all these errors... java.io.NotSerializableException: Item at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source) at java.util.Hashtable.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 Test.main(Test.java:143) I'm not sure what most of these errors mean or why I am getting them or even how to fix them. Can anyone help me? EDIT: STORE CLASS ``` import java.io.Serializable; import java.util.Hashtable; public class Store implements Serializable{ Hashtable<String, Item> stockedItems = new Hashtable<String, Item>(); public boolean addItem(String code){ if(stockedItems.containsKey(code)){ stockedItems.get(code).incrementQuantity(); return true; } return false; } public boolean removeItem(String code){ if(stockedItems.containsKey(code)){ stockedItems.get(code).decrementQuantity(); return true; } return false; } public boolean findItem(String code){ if(stockedItems.containsKey(code)){ return true; } return false; } } ``` **My HashTable holds Item Objects which do not implement Serializable. Which I now fixed. Program runs and Q case works fine! Now it is my U case that does not work and here it is... ``` case "U": try{ FileInputStream fis = new FileInputStream("table.obj"); ObjectInputStream ois = new ObjectInputStream(fis); store = (Store)ois.readObject(); ois.close(); }catch(IOException | ClassNotFoundException e){ e.printStackTrace(); } break; ``` The purpose of this case is to allow the user to choose whether or not they want to use the data stored in my .obj file or not. I get these errors upon attempting to use that case at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source) at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.readObject(Unknown Source) at Test.main(Test.java:142)