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

