java.io.NotActiveException

package serialization;

import java.io.*;

/**

* DOCUMENT ME!

*

* @author$author$

* @version $Revision$

*/

publicclass First

implements Serializable

{

//~ Constructors ...........................................................................................................

/**

* Creates a new First object.

*

* @param x DOCUMENT ME!

*/

public First(int x )

{

selectedValue = x;

}

//~ Methods ................................................................................................................

/**

* DOCUMENT ME!

*

* @param args DOCUMENT ME!

*/

publicstaticvoid main( String args[] )

{

new First( 15 );

First first =new First( 20 );

first.Method( first );

}// end method main

//~ ........................................................................................................................

/**

* DOCUMENT ME!

*

* @param object DOCUMENT ME!

*/

publicvoid Method( First object )

{

try

{

notSerializable =new NotSerializable( 50 );

File file =new File("serialised.text" );

FileOutputStreamstream =new FileOutputStream( file );

ObjectOutputStream o =new ObjectOutputStream( stream );

writeObject( o );

}

catch( IOException e )

{

e.printStackTrace();

}

try

{

FileReader fileReader=new FileReader("serialised.text" );

FileInputStreaminputStream=new FileInputStream("serialised.text" );

ObjectInputStream objectInputStream =new ObjectInputStream( inputStream );

readObject( objectInputStream );

}

catch( IOException e )

{

e.printStackTrace();

}

}// end method Method

//~ ........................................................................................................................

/**

* DOCUMENT ME!

*

* @param is DOCUMENT ME!

*/

privatevoid readObject( ObjectInputStream is )

{

try

{

is.defaultReadObject();

NotSerializable newObject =new NotSerializable( is.readInt());

Firstobjects= (First) is.readObject();

System.out.println("retrieved value is : " + objects.selectedValue );

System.out.println("retrieved value from the other class is : " + newObject.getValue());

}

catch( IOException e )

{

e.printStackTrace();

}

catch( ClassNotFoundException e )

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

privatevoid writeObject( ObjectOutputStream o )

{

try

{

o.defaultWriteObject();

o.writeInt( notSerializable.getValue());

}

catch( IOException e )

{

}

}

//~ Instance variables .....................................................................................................

/**

* Field DOCUMENT ME!

*/

privatetransient NotSerializable notSerializable =null;

/**

* Field DOCUMENT ME!

*/

privateint selectedValue = 0;

}// end class First

package serialization;

publicclass NotSerializable

{

public NotSerializable(int x )

{

value = x;

}

publicint getValue()

{

return value;

}

publicvoid setValue(int value )

{

this.value = value;

}

privateint value;

}// end class NotSerializable

When I run First.java

The output that I get is :

java.io.NotActiveException: not in call to readObject

at java.io.ObjectInputStream.defaultReadObject(Unknown Source)

at serialization.First.readObject(First.java:153)

at serialization.First.Method(First.java:134)

at serialization.First.main(First.java:61)

Can someone explain me how to fix the issue

[7473 byte] By [jaaya] at [2007-10-3 2:41:55]
# 1
What's line 153?
CeciNEstPasUnProgrammeura at 2007-7-14 19:40:38 > top of Java-index,Java Essentials,New To Java...
# 2
You can't call your own readObject() and writeObject() methods directly. Instead you must call out.writeObject(this) and in.readObject().
ejpa at 2007-7-14 19:40:38 > top of Java-index,Java Essentials,New To Java...
# 3

> You can't call your own readObject() and

> writeObject() methods directly. Instead you must call

> out.writeObject(this) and in.readObject().

I made the changes and the code works fine.

But who calls the readObject method?

I had a look at the API which states that readObject and writeObject method can be overridden.

So I assume that I overrode it but from whome? since the class does

not extend ObjectInputStream and ObjectOutputStream

jaaya at 2007-7-14 19:40:38 > top of Java-index,Java Essentials,New To Java...
# 4
ObjectInputStream.readObject() calls your readObject() method if it exists, and ObjectOutputStream.writeObject() calls your writeObject() method if it exists.Yes I know they are private, for security reasons: they use some magic to find and call them.
ejpa at 2007-7-14 19:40:38 > top of Java-index,Java Essentials,New To Java...
# 5
> Yes I know they are private, for security reasons:> they use some magic to find and call them.what is that magic?. I tried to find that but couldnt
jaaya at 2007-7-14 19:40:38 > top of Java-index,Java Essentials,New To Java...
# 6
Probably Reflection.
CeciNEstPasUnProgrammeura at 2007-7-14 19:40:38 > top of Java-index,Java Essentials,New To Java...
# 7
> > Yes I know they are private, for security reasons:> > they use some magic to find and call them.> > what is that magic?. I tried to find that but couldntOnly the Ancients are supposed to have the power that comes with such arcane
jwentinga at 2007-7-14 19:40:38 > top of Java-index,Java Essentials,New To Java...