【发布时间】:2021-11-05 00:25:15
【问题描述】:
我正在尝试从文件中反序列化平面对象,但出现此错误:
java.lang.ClassCastException:无法将 hausverwaltung.EigentumsWohnung 类强制转换为 java.util.List 类(hausverwaltung.EigentumsWohnung 在加载器“app”的模块 hausverwaltung 中;java.util.List 在加载器的模块 java.base 中'引导')
这是我的代码:
public List<Flat> getFlats() {
List<Flat> flats = new ArrayList<Flat>();
try {
ObjectInputStream reader;
reader = new ObjectInputStream(new FileInputStream(file));
flats = (List<Flat>) reader.readObject();
reader.close();
} catch (Exception e) {
System.out.println("Deserialisation error: "+e);
System.exit(1);
}
return flats;
}
这是我们在课堂上学习的方式,但不知何故我得到了这个错误。 我整天都在寻找解决方案,但没有找到任何解决方案……我该如何解决这个问题?
【问题讨论】:
-
你正在将 Flat 投射到 List
-
要反序列化对象列表,您需要先序列化对象列表,而不是单个对象。
-
谢谢!我认为现在它可以工作了
标签: java