• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Getting an exception(Serialization)

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below is from the k&B.


Why am I getting an exception when I deserialize?. Ive run through this code but still cant see what im doing wrong. Please help.
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Above code serilize and deserilize a transient variable.
comment and uncomment private writeObject and readObject methods and see the output.
I hope it will help You.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Transient variables are not Serializable. If transient variable is a primitive and you try to deserialize it, java will initialize the primitive with the default value, for an Object it will be null.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
G'day mates!

Three errors, the wrong version is outcommented:
// -->private void WriteObject(ObjectOutputStream os) throws IOException {
private void writeObject(ObjectOutputStream os) throws IOException {try {


//private void ReadObject(ObjectInputStream is) throws IOException,
private void readObject(ObjectInputStream is) throws IOException,ClassNotFoundException {


this method names must be lower case, and the third in the constructor:


public Dog(Collar collar, int size) {
theCollar = collar;
//size = dogSize;
dogSize=size;
}
just the other way round.

Yours,
Bu.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic