Java 可序列化问题

发布于 2024-12-21 03:43:21 字数 3074 浏览 5 评论 0原文

我正在尝试在程序中序列化我的模型。该模型名为“ImageModel”,并且实现了Serialized。该模型还包含另一个名为“Perspective”的自定义对象,该对象也实现了Serialized。我有一个静态实用程序类,可以序列化我的模型并读取它。此代码已在主方法中使用“ImageModel”进行了测试,一切正常。

但是当我尝试在实际程序中使用它时,我遇到了问题。 “ImageModel”类在我的系统类中声明,名为“MainWindow”,它扩展了JFrame并且是大多数不同类之间的链接。由于某种原因,我无法序列化模型以执行 MainWindow.getModel() 之类的操作。编译器认为我的“EventFactory”不可序列化。这个类也在“MainWindow”中声明,但我什至不明白为什么Java想要序列化它,我的印象是java不仅试图序列化模型,而且还尝试序列化模型。图形用户界面。

以下是代码段:

我的模型:

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;
...
}

透视图类:

public class Perspective implements Serializable {

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

声明模型和其他元素的实际 GUI:

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;
 ...
 }

main 方法:

    MainWindow mw = new MainWindow();

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

这是我的两个实用函数,以防您需要它们:

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;
}

这是 STACK_TRACE我在处理实际用例时收到的错误:

http://pastie.org/3008549

所以是的,就像我说的,这就像 Java 试图序列化模型周围的其他东西一样。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光下的泡沫是彩色的 2024-12-28 03:43:21

我猜 EventFactory 正在以某种方式进入 ImageModel 的字段。也许是从观察者间接链接的。也许您应该在尝试序列化或将该字段设置为瞬态之前清除该列表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文