简体   繁体   中英

Java Serializable Issue

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM