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?
klynn47@comcast.net - 13 Mar 2006 18:33 GMT
Does the class definition of Price History implement Serializable?
Colin Hemmings - 13 Mar 2006 19:13 GMT
> 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
opalpa@gmail.com opalinski from opalpaweb - 13 Mar 2006 19:18 GMT
> 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
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Colin Hemmings - 13 Mar 2006 20:25 GMT
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.
>>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
[quoted text clipped - 10 lines]
> opalpa@gmail.com
> http://www.geocities.com/opalpaweb/
Thomas Hawtin - 13 Mar 2006 21:22 GMT
> 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

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Thomas Hawtin - 13 Mar 2006 18:59 GMT
> 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
[quoted text clipped - 6 lines]
> 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

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Roedy Green - 13 Mar 2006 19:00 GMT
On Mon, 13 Mar 2006 17:20:42 GMT, Colin Hemmings
<colin.hemmings1@ntlworld.com> wrote, quoted or indirectly quoted
someone who said :
>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

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.