Jump to content

Serialization and Deserialization problems

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
moody

moody

    Newbie

  • Members
  • Pip
  • 1 posts
Hello!

I'm having some problems regarding serialization and polymorphic arguments.
My project has an array of Aquariums and I want to serialize each one to later deserialize them.
Below is the Sim constructor from Sim class which deals with Aquarium serialization.
I don't really know why the o.writeObject(aqua) doesn't work as intended(which is to serialize an Aquarium which is given as a polymorphic argument).Neither does aq=(Aquarium) in.readObject().
I suspect it has something to do with the polymorphic arguments being passed on the methods.


Sim{

...

for(int i=0;i<aquariumssize();i++)

         serializeAquarium(aquariums.get(i));

}


public void serializeAquarium(Aquarium aqua)

{

   

  try {

      System.out.println(aqua.getNameAquarium());

      ObjectOutputStream o=new ObjectOutputStream(new FileOutputStream(aqua.getNameAquarium()+".dat"));

      o.writeObject(aqua);

      o.close();

      }

   catch (Exception e)

       {

       System.out.println("Couldn't not serialize!");

       }

    

}

public Aquarium deserializeAquarium(String name)

{

     Aquarium aq=null;

     

  try {

      FileInputStream fileIn =new FileInputStream(name+".dat");    

      ObjectInputStream in = new ObjectInputStream(fileIn);


      aq=(Aquarium) in.readObject();

      

      in.close();


       }

   catch (Exception e) 

   {

       System.out.println("Couldn't deserialize!");


   }

  return aq;

}

Thanks in advance for the help!

#2
ksemeks

ksemeks

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
It would help if you'd put e.printStackTrace() in the catch block.
// d-_-b+

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Your Aquarium class implements the serializable interface, right?

Public class Aquarium implements Serializable