Results 1 to 8 of 8

Thread: Serializable Class

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Resolved Serializable Class

    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

    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);
        }
    }
    now what functions do I need to add to make it Serializable ?
    I added the implements Serializable on top of my class and then I tried the following functions ...

    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]);
            }
        }
    When I run this I get an error at the following line:
    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
    Last edited by BramVandenbon; Oct 28th, 2004 at 06:56 PM.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width