aspose file tools*
The moose likes I/O and Streams and the fly likes Serialization problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Serialization problem" Watch "Serialization problem" New topic
Author

Serialization problem

Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
I am unable to run the following code.it gives me a compile time error.please help me solve this.i guess i am making a basic mistake but cannot figure it out.
Thanks in advance

import java.io.*;
class base
{
int x=8;
}
class base1 extends base implements Serializable
{
private transient base b=new base();

}

public class sirhir
{
base b;
public static void main(String str[]) throws IOException,ClassNotFoundException
{


base1 b1=new base1();
FileOutputStream frs=new FileOutputStream("nawa.ser");
ObjectOutputStream oos=new ObjectOutputStream(frs);
oos.writeObject(oos);

oos.close();

FileInputStream fis=new FileInputStream("nawa.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
base1 bs1=(base1)ois.readObject(ois);
ois.close();



}
private void writeObject(ObjectOutputStream oo)
{
try
{
oo.defaultWriteObject();
oo.writeInt(b.x);
}
catch(Exception e)
{
System.out.println(e);
}
}

private void readObject(ObjectInputStream inp)
{
try
{
inp.defaultReadObject();
inp.readInt(b.x);
}
catch(Exception e)
{
System.out.println(e);
}
}
}


Jitendra
SCJP1.5
SCWCD1.5
krishnamoorthy kitcha
Ranch Hand

Joined: Feb 04, 2006
Posts: 96
Hi Jitendra Jha


Everything shoulb be correct , Remove the transient key word then try to execute your code
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Change the lines

into


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
The Program is still throwing an exception.
I am getting not serializable exception
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

The problem is in the "oos.writeObject(oos);" line; you're writing the output stream to itself, but the output stream isn't serializable.

Turn it into "oos.writeObject(b1);" and it will work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization problem