• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting an exception(Serialization)

 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • 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.
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • 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.
 
Patrick Patel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic