• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Serialization problem

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
}
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jitendra Jha


Everything shoulb be correct , Remove the transient key word then try to execute your code
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change the lines

into
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Program is still throwing an exception.
I am getting not serializable exception
 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
It's feeding time! Give me the food you were going to give to this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic