【发布时间】:2014-02-15 00:32:53
【问题描述】:
我在 src 中有三个包:模型、视图和控制器。 视图有几个 JFrame,模型有几个处理程序逻辑的类,控制器有一个包含视图中 JFrame 实例的类,而模型中只有一个对象,称为“板”。 我应该提到,在模型中有一个名为 Saver 的类,它只有一个静态方法来加载和一个静态方法来保存这个对象'board'。
当我尝试序列化对象“板”(板和模型中的所有类都是可序列化的)时,我从视图中的所有类以及控制器中的所有内部类中进入堆栈跟踪 NotSerializableException类,当我只尝试序列化属于模型的“板”时,所有板的变量都是可序列化的。
以下是从 Saver 类中保存和加载的方法:
public class Saver {
public static void saveBoard(Board board) throws IOException{
FileOutputStream fileOutputStream = new FileOutputStream("gosavefile.gsf");
ObjectOutputStream obout = new ObjectOutputStream(fileOutputStream);
obout.writeObject(board);
fileOutputStream.close();
obout.close();
}
public static Board loadBoard() throws IOException, ClassNotFoundException{
FileInputStream fileInputStream = new FileInputStream("gosavefile.gsf");
ObjectInputStream obin = new ObjectInputStream(fileInputStream);
Board board = (Board) obin.readObject();
fileInputStream.close();
obin.close();
return board;
}
}
这就是我从控制器类调用 saveBoard() 方法的地方:
public void save(){
try{
Saver.saveBoard(board);
} catch (IOException e) {
new SaveErrorWindow(mainMenuWindow.getLanguageManager());
e.printStackTrace();
}
}
我以前做过一次这样的事情,另一个程序使用相同的确切方案,并且效果很好。所以我真的不明白为什么当我只序列化'board'时它会尝试序列化其他所有内容。
如果需要代码的任何其他部分,请询问。
提前谢谢你!
【问题讨论】:
-
向我们展示
Board的源代码 -
Board可能引用了一个不可序列化的类(甚至可能通过其他一些类)。 -
这个问题确实不完整。阅读minimal reproducible example,下次做得更好。
标签: java serialization notserializableexception