Sherifftwinkie
Sherifftwinkie

Reputation: 391

Serializing a HashTable, Java

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)

Upvotes: 6

Views: 6668

Answers (3)

Mike Samuel
Mike Samuel

Reputation: 120526

"java.io.NotSerializableException: Item" is saying that class Item is not serializable. It needs to be serializable because the contents of a map need to be serializable for the whole map to be serializable.

Upvotes: 1

Vivin Paliath
Vivin Paliath

Reputation: 95558

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

Upvotes: 10

Lee Meador
Lee Meador

Reputation: 12985

You can only write objects that are Serializable and you can look up Oracle's JavaDoc for that and learn the details.

In general, you add implements Serializable to any class that meets a few requirements, in most cases.

A class has to only have fields that are either themselves Serializable objects or some of the raw types like int or char, etc.

If there are fields that are of some super-type, the object instance occupying that field has to be Serializable even though the super-type isn't.

There is lots more to it.

Upvotes: 1

Related Questions