dcsimg
Serializing an object from an array?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Murugan_Pitchairaman
Posted On:   Thursday, February 28, 2002 09:19 PM

Hi, I am trying to serialize an object taken out of an array. The following code doesn't work. public class Test implements Serializable { class Test(){} // rest } class Foo{ Test[] test = new Test[10]; //just for test i make 10 objects of Test File filename = new File(dirName,"foo.bin"); ObjectOutputStream objectOut = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream(filename))); objectOut.writeObject(test[0]);//trying to store the first objectOut.close(); } The same   More>>

Hi,



I am trying to serialize an object taken out of an array. The following code doesn't work.



public class Test implements Serializable

{

class Test(){}


// rest

}




class Foo{



Test[] test = new Test[10]; //just for test i make 10 objects of Test



File filename = new File(dirName,"foo.bin");


ObjectOutputStream objectOut = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream(filename)));



objectOut.writeObject(test[0]);//trying to store the first

objectOut.close();


}



The same code works if I try for just one staight sigle object. Does name have to do anything since
array will have the same name for different objects? (but they have unique index though) Could anyone please let me know what I am doing wrong here? Thanks.

   <<Less

Re: Serializing an object from an array?

Posted By:   Duncan_Colvin  
Posted On:   Saturday, March 16, 2002 03:27 PM

When you create the array you are creating an array of ten references to Test objects and not ten instances of Test object. You would need to create a new object for each element of the array, otherwise there will be no objects in the array to serialize.




test[0] = new Test();

test[1] = new Test();

...

...





I hope this helps.



DBC

About | Sitemap | Contact