1

I'm trying to serialize my model inside my program. The model is named "ImageModel" and it implements Serializable. This model also contains another custom object named "Perspective" which is also implementing Serializable. I have a static utility class that serializes my model and reads it. This code has been tested in the main method with an "ImageModel" and everything is working perfectly.

But when I try to use it in the actual program I'm running into an issue. The "ImageModel" class is declared in my system class, named "MainWindow" which extends JFrame and is the link between most of the different classes. For some reason, I can't serialize the model doing something like MainWindow.getModel(). The compiler argues that my "EventFactory" is not serializable. This class is also declared in "MainWindow", but I'm not even understanding why Java wants to serialize it, I'm under the impression that java is not just trying to serialize the model, but also the GUI.

Here are segments of code :

My model:

public class ImageModel extends Observable implements Cloneable, Serializable {

private String path;
private ArrayList<Observer> observers;
private ArrayList<Perspective> perspectives;
private int numPerspectives;
private Perspective selectedPerspective;
...
}

The perspective class:

public class Perspective implements Serializable {

private ImageModel image;
private int degreeOfRotation;
private Point topLeftPoint;
private int zoomPercentage;
private int height;
private int width;
 ...
 }

The actual GUI that declares the model and other elements:

public class MainWindow extends JFrame {

    private final int GRID_ROWS = 0;
    private final int GRID_COLUMNS = 2;
    private final int NUM_PERSPECTIVE = 3;
    private JPanel mainPane;
    private ArrayList<View>  perspectiveList;
    private ImageModel imageModel;
    private EventFactory eventFactory;
    private JMenu menu;
    private JToolBar toolBar;
 ...
 }

The main method:

    MainWindow mw = new MainWindow();

    /*
     * Does NOT work:
     * ImageModel imageModel= mw.getImageModel();
     * Utility.serializeModel(imageModel); //Crashes
     * 
     * Works:
     * 
     * ImageModel imageModel= new ImageModel();
     * Utility.serializeModel(imageModel);
     * 
     */

Here are my two utility functions in case you need them :

public static void serializeModel(ImageModel imageModel)
{
    String filename = "TEST.ser";

    FileOutputStream fos = null;
    ObjectOutputStream out = null;

    try
    {
        fos = new FileOutputStream(filename);
        out = new ObjectOutputStream(fos);
        out.writeObject(imageModel);
        out.close();
    }
    catch (IOException ex) 
    {
        ex.printStackTrace();
    }

}

public static ImageModel restoreModel(String filename)
{
    ImageModel imageModel = null;
    FileInputStream fis = null;
    ObjectInputStream in = null;
    try
    {
        fis = new FileInputStream(filename);
        in = new ObjectInputStream(fis);
        imageModel = (ImageModel)in.readObject();
        in.close();
    }
    catch(IOException ex)
    {
        ex.printStackTrace();
    }
    catch(ClassNotFoundException ex)
    {
        ex.printStackTrace();
    }

    return imageModel;
}

Here's the STACK_TRACE of the error I'm receiving when working on the actual use case:

http://pastie.org/3008549

So yeah, like I'm saying, it's like if Java was trying to serialize other stuff around the model.

2
  • not really an answer, but if you want to workaround it : change the EventFactory to a transient data member. Ex: "private transient EventFactory eventFactory;" Commented Dec 13, 2011 at 3:46
  • 1) For better help sooner, post an SSCCE. 2) Put at least the first 50 or so lines of a stack trace directly in the question. Most people won't follow a link. Commented Dec 13, 2011 at 3:46

1 Answer 1

4

I'm guessing EventFactory is somehow making it's way into ImageModel's fields. Maybe indirectly linked from an Observer. Perhaps you should clear that list before attempting to serialise or set that field as transient.

Sign up to request clarification or add additional context in comments.

2 Comments

maybe the question should be "Is MainWindow an observer of ImageModel?"
Ok first, MainWindow is not an obersever, I have views elsewhere that implement Observer. BUT, @Steven, I just found out exactly what you mentioned! The actual issue resided in the ImageModel. IT holds an array of Observers which are views that hold a reference to MainWindow! So it was indeed trying to serialize the whole class! Alright thanks guys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.