File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on 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 "Doubt on Serialization?" Watch "Doubt on Serialization?" New topic
Author

Doubt on Serialization?

Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

My program is compiles but throws the following exception at runtime:-
java.io.NotActiveException: not in call to writeObject
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413)
at Test.writeObject(Test.java:10)
at Test.main(Test.java:23)

please explain me why it throws NotActiveException,I am confused....



[ August 10, 2008: Message edited by: Ashok Pradhan ]

[ August 10, 2008: Message edited by: Ashok Pradhan ]
[ August 10, 2008: Message edited by: Ashok Pradhan ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
The javadocs of the defaultWriteObject method explain why you're getting this exception - you're not serializing the current object.

I think what you may mean to call is "os.writeObject(new Test())". Then you need to implement the writeObject method as explained in the javadocs for Serializable.


Ping & DNS - updated with new look and Ping home screen widget
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

In my program the class Dog does not implements Serializable interface:-




but I want manually write doglegs by serialization as well as all the members of Test class(e.g Cat c=new Cat()) :-



but how can I invoke the writeObject() it throws NotActiveException and what are the changes needed to fulfill my program's requirement.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

but how can I invoke the writeObject() it throws NotActiveException and what are the changes needed to fulfill my program's requirement.


Basically, you are *not* supposed to call the writeObject() method directly. It is a method used to support Serialization. See Ulf explanation on how to serialize an object. The serialization will indirectly call the writeObject() method.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

Now I got it.invoking readObject(ObjectOutputStream os) directly is the wrong process and it is automatically called in the serialization process.am I correct !!!.and my program now compiles and run successfully..



Thanks Ulf Dittmer and Henry !!!
[ August 10, 2008: Message edited by: Ashok Pradhan ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt on Serialization?
 
Similar Threads
Serialization JSCP 6.0 (manually invoking writeObject() )
private(?) writeObject/readObject
Serialization
How it works when we implement writeObject and readObject
Serializable Problem