Welcome to Dream.In.Code
Become a Java Expert!

Join 149,709 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,265 people online right now. Registration is fast and FREE... Join Now!




NeedSuggestion:Serialization

 
Reply to this topicStart new topic

NeedSuggestion:Serialization

Ryan Smith
14 Feb, 2007 - 09:45 AM
Post #1

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 28


My Contributions
I have a class hierachy that needs to be serialized into a single file... but I'm having some trouble with getting the serialization right.

Lets say this is the tree:
class Manager
-----has an ArrayList of class SubClass1
-------------which has an ArrayList of class SubClass2
---------------------which contains ArrayLists of differing data.

I would like the class Manager to be serialized into one file.
I think I read somewhere ArrayLists do not serialize... so before serialization is attempted, the arraylist.toArray() is called in each class, and each class' ArrayList is stored in an internal Object[].

All that the file this is serialized to contains is the variable names of class Manager.
So, I am guessing that serialization will not take this hierarchy down into one file.
Am I doing something wrong? Or should I be using a method other than serialization to store this hierarchy?

Due to licensing I cannot release code files on the project, but I could include snippets. Any help would be greatly appreciated.
User is offlineProfile CardPM
+Quote Post

horace
RE: NeedSuggestion:Serialization
14 Feb, 2007 - 10:31 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
ArrayList implements Serializable so you can write and read it using writeObject() and readObject(). However, the elments stored in the ArrayList must have implemented Serializable or you will get the error message
java.io.NotSerializableException: TestObject

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: NeedSuggestion:Serialization
14 Feb, 2007 - 11:26 AM
Post #3

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
very simply your SubClass2 class must implement serializable so that the objects can be stored in the arraylist properly. Any changes to the code after saving it (if you save it) will make the objects impossible to read, due to the serializable though.
User is offlineProfile CardPM
+Quote Post

Ryan Smith
RE: NeedSuggestion:Serialization
14 Feb, 2007 - 09:22 PM
Post #4

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 28


My Contributions
Surely everyone experienced in coding... among everything else... is experienced in Murphy's Law, and Blonde's Law.

If you want read something that's deserialized, make sure the object you serialize is the same instantiation you expect to read from later. The object being serialized was a debugging test blank instanciation object I neglected to change.

Thank you for your help. Confirming me on serialization made me look elsewhere for my problem. Thank you.

This post has been edited by Ryan Smith: 14 Feb, 2007 - 09:32 PM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: NeedSuggestion:Serialization
15 Feb, 2007 - 04:58 AM
Post #5

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
sometimes a little confirmation is all that is needed. Glad to see the problem is solved smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 04:27AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month