HashTable serialization, Java

I have never used serialization before. I think everything is fine with me, except for the last part in my Q-case.

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 means it contains 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 try to save all the data in my .obj file and close the streams and exit my program, I get all these errors ...

java.io.NotSerializableException: an element in java.io.ObjectOutputStream.writeObject0 (Unknown source) in java.io.ObjectOutputStream.writeObject (Unknown source) in java.util.Hashtable.writeObject (Unknown source) at sun.Imativefref. invoke0 (native method) at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown source) at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown source) in java.lang.reflect.Method.invoke (Unknown source) in java.ioClass Object. invokeWriteObject (Unknown source) in java.io.ObjectOutputStream.writeSerialData (Unknown source) in java.io.ObjectOutputStream.writeOrdinaryObject (Unknown source) in java.io.ObjectOutputStream.writeObject0 (Unknown ik) in java.io.ObjectOutputStream.defaultWriteFields (Unknown source) in java.io.ObjectOutputStream.writeSerialData (Unknown source) in java.io.ObjectOutputStream.writeOrdinaryObject (Unknown source) in java.io.ObjectOwrite.ObjectOwriteObjectObject in java.io.ObjectOutputStream.writeObject (Unknown source) in Test.main (Test.java:143)

I'm not sure what the majority of these errors mean or why I get them or even correct them. Can anybody 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 contains Item objects that do not implement Serializable. Which I have now fixed. Running the program and working Q is great! Now this is my case U, which 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 they want to use the data stored in my .obj file or not. I get these errors when trying to use this case

in java.io.ObjectInputStream $ BlockDataInputStream.peekByte (Unknown source) in java.io.ObjectInputStream.readObject0 (Unknown source) in java.io.ObjectInputStream.readObject (Unknown source) in Test.main (Test.java:142)

+6
source share
3 answers

Even if the Hashtable is serializable, the objects you store inside it must also be serializable. So I would first check if what you store in your Hashtable will implement 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 have to implement Serializable . In fact, this is exactly what the first line of the exception says:

 java.io.NotSerializableException: Item 
+10
source

You can only write Serializable objects, and you can find Oracle JavaDoc for this and find out more.

In general, you add implements Serializable to any class that in most cases satisfies several requirements.

A class should have only fields, which are either Serializable objects themselves, or some raw types, such as int or char , etc.

If there are fields that have a certain supertype, the instance of the object occupying this field must be Serializable, even if the supertype is not.

There are many more.

+1
source

"java.io.NotSerializableException: Item" says the class Item not serializable. It must be serializable because the contents of the map must be serializable for the entire map that can be serialized.

+1
source

Source: https://habr.com/ru/post/943471/


All Articles