File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Getting an exception(Serialization) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Getting an exception(Serialization)" Watch "Getting an exception(Serialization)" New topic

Getting an exception(Serialization)

Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
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

Joined: Feb 07, 2005
Posts: 643

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

Joined: Aug 25, 2006
Posts: 9
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

Joined: Aug 25, 2006
Posts: 1274
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;
just the other way round.


all events occur in real time
I agree. Here's the link:
subject: Getting an exception(Serialization)
It's not a secret anymore!