I have a class that needs to implement the "Serializable" interface. I am doing this because I am saving an instance of my class to a binary file. And in order to do that it needs to be Serializable.
The name of the class is "Animation". It is a collection of images.
And has a lot of functions to make an animation of these images. I am not going to mention those here though. Because they have nothing to do with my question.
My class looks a bit like this
now what functions do I need to add to make it Serializable ?Code:public class Animation(){ ArrayList files; // a collection of Strings (the path of the imagefile) ArrayList images; // a collection of BufferedImages public Animation(){ //a constructor } public void addFile(String file){ //function to add new Images images.add(loadimages(file)); files.add(file); } }
I added the implements Serializable on top of my class and then I tried the following functions ...
When I run this I get an error at the following line:Code:private void writeObject(java.io.ObjectOutputStream out) throws IOException{ out.writeObject(files); } private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException{ String[] imported_files = (String[])in.readObject(); setImageArray(imported_files); } private void setImageArray(String[] imported_files){ images.clear(); files.clear(); for(int i=0;i<imported_files.length;i++){ images.add(loadimages(imported_files[i])); files.add(imported_files[i]); } }
String[] imported_files = (String[])in.readObject();
It says that the casting to String[] is not possible. I tried the same thing with int[] in another project and there it worked fine.a bit strange.
I only get this error when i am trying to read my instance from a file. Writing to the file works without errors. (Allthough I can not be sure it works because I have never been able to load it again).
Why do I get this error and how can I fix it?
Thank you in advance![]()




a bit strange.
Reply With Quote