首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java可序列化问题

Java可序列化问题
EN

Stack Overflow用户
提问于 2011-12-13 11:34:51
回答 1查看 470关注 0票数 1

我正在尝试在我的程序中序列化我的模型。该模型名为"ImageModel“,它实现了Serializable。该模型还包含另一个自定义对象"Perspective“,该对象也实现了Serializable。我有一个静态实用类,它序列化我的模型并读取它。这段代码已经在main方法中使用"ImageModel“进行了测试,一切工作正常。

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

以下是代码片段:

我的模型:

代码语言:javascript
复制
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;
...
}

透视图类:

代码语言:javascript
复制
public class Perspective implements Serializable {

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

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

代码语言:javascript
复制
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方法:

代码语言:javascript
复制
    MainWindow mw = new MainWindow();

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

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

代码语言:javascript
复制
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试图序列化模型中的其他东西一样。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-13 11:48:14

我猜EventFactory正以某种方式进入ImageModel的字段。可能是间接链接到一个Observer。也许你应该在尝试序列化该字段或将其设置为transient之前清除该列表。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8484018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档