• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on Serialization?

 
Ashok Pradhan
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashok Pradhan
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21022
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ashok Pradhan
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic