Serialization

C

Colin Hemmings

Hi there,
I am having a problem with serialization.

I have an ArrayList called 'stocks' which contains 'stock' objects each
stock contains a 'price history' class.

I run serialization on the ArrayList by writing the stocks arraylist to
a file when the list is changed and reading the arraylist backing each
time the main class is run.

The serialization works fine for storing the details of a stock (like,
stockName, currentPrice etc) but its isnt storing the price history
object that each stock contains.

Why is this? I assumed serialization would store the entire contents of
each stock?
 
T

Thomas Hawtin

Colin said:
I have an ArrayList called 'stocks' which contains 'stock' objects each
stock contains a 'price history' class.

'price history' object?
I run serialization on the ArrayList by writing the stocks arraylist to
a file when the list is changed and reading the arraylist backing each
time the main class is run.

The serialization works fine for storing the details of a stock (like,
stockName, currentPrice etc) but its isnt storing the price history
object that each stock contains.

Why is this? I assumed serialization would store the entire contents of
each stock?

It difficult to say without seeing the code. But it could be (some more
likely than others) -

o The price history field is marked transient.
o writeObject method fails to write the price history.
o readObject method fails to read the price history.
o readObject reads the price history and then overwrites it.
o Price history field is in an unserialisable subclass.
o Price history has an odd readResolve/writeReplace method.
o You are doing something subtly different from what you think you are
doing (always a good candidate when the impossible happens).

Tom Hawtin
 
R

Roedy Green

The serialization works fine for storing the details of a stock (like,
stockName, currentPrice etc) but its isnt storing the price history
object that each stock contains.

All the objects you want to store should implement Serializable.

It won't store those objects if the references are marked transient.
It is then up to you to restore them on read.

see http://mindprod.com/jgloss/serialization.html
 
C

Colin Hemmings

Does the class definition of Price History implement Serializable?
I have tried that and it compiles but because I'm using RMI when I goto
run the server it causes it to throws up some funny error to do with the
pricehistory.

I really dont know what to try
 
O

opalpa

I have tried that and it compiles but because I'm using RMI when I
goto run the server it causes it to throws up some funny error

Server and client need same version of classes. And if you want
something serialized it needs to be a Serializable like klyn... said
I really dont know what to try

So you don't want to persue the root of the RMI error but try a
different approach all together?

Opalinski
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 
C

Colin Hemmings

I do want to pursue the RMI error but I assmued that was coming up
because I was doing something wrong. It seems as though itnot letting me
make price history serializable because I'm using observer and
observable aswell on the server.
 
T

Thomas Hawtin

Colin said:
I do want to pursue the RMI error but I assmued that was coming up
because I was doing something wrong. It seems as though itnot letting me
make price history serializable because I'm using observer and
observable aswell on the server.

Observable isn't serialisable itself. I suggest you ditch it in favour
of 1.1-style events.


If you want to stick with Observable, or just for reference:

As Observable is subclassable and has an accessible no-args
contructor[1], it can become serialisable by subclassing. However, it
will not store or restore its observers. It would seem you need to
provide a readObject and writeObject. However, you don't have access to
the observers. Therefore, you will need to override and duplicate the
functionality of addObserver, deleteObserver and deleteObservers. I
suggest you don't.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top