为什么我在尝试序列化对象时会收到NotSerializable Exception
我有以下课程:
public class GameWorld implements Serializable {
int sumu_dist = 30;
public List<Tiles> tileGroup = new ArrayList<>();
public List<Tiles> loadingTiles = new ArrayList<>();
// constructor etc.
}
在另一堂课中,我尝试保存 gameworld
,但没有序列化的例外:
public class game {
static GameWorld GameWorldObj = new GameWorld();
String FileName = "WorldData.bin";
void Save(){
try {
FileOutputStream FOS = new FileOutputStream(FileName);
ObjectOutputStream OOS = new ObjectOutputStream(FOS);
if (gameworld_obj instanceof Serializable){
OOS.writeObject(GameWorldObj); // java.io.NotSerializableException
}
System.out.printf("SAVED: %s \n", OOS);
OOS.close();
} catch (IOException exception){
System.err.println(String.valueOf(exception));
}
}
public static void main(String[] args) {
new game().Save();
}
我不知道为什么会发生,我会搜索并找到一些答案,告诉您要实现实现序列化
。我做到了,但我仍收到 notSerializableException
。
为什么会发生?我该怎么办?
错误消息:
java.io.NotSerializableException: source.library.level.Tiles
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1197)
at java.base/java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1582)
at java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1539)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1448)
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1191)
at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:354)
at source.library.level.game.Save(game.java:33)
at source.library.level.game.main(game.java:47)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
整个对象图需要序列化,如果至少一个字段未实现
序列化
接口,则在尝试序列化此类实例时会得到异常。引用来自:
JDK中内置的原始类型和大多数数据类型都是可序列化的。
您可以使用这些类型遇到麻烦 :
对象
不实现serializable
,因此object
类型字段是在班级中不允许序列化。可选
以及optionalInt
,optionallong
和optianaldouble
不可序列化,因为将可选类型引入了有限的机制对于返回类型,而不是打算用作字段。
Tiles
类必须实现序列化
,如果Tiles内部有一些自定义字段类型
,它们也必须序列化。如果您不希望特定字段被序列化,则可以将其标记为
瞬态
。在序列化过程中忽略了瞬态字段以及静态
字段。因此,如果您将字段
tileGroup
和loadingtiles
为transient
,那么tiles
class class becals n demboce n n demboce n n the Compand n n the Code> both in Code> the code>实现序列化
,因为这些属性将从序列化版本中排除。作为避难所后的一致性,它们将被初始化为null
。通常,序列化将用于临时数据存储或传输数据。为了确保从内存中检索的对象与 .class 文件兼容,您有一个很好的做法来定义
静态最终long serialversionuid
字段,该字段表示当前版本班级。,您还应该意识到,序列化具有一些严重的绘图(<<<<<<<< em>查看上面提供的链接)。
sidenote :class-names通常是单数名词喜欢
student
,雇员
,<代码>图块,因为它们的实例旨在表示一个对象。The whole object graph needs to be serializable, if at least one of the fields doesn't implement
Serializable
interface you'll get an exception while attempting to serialize the instance of this class.Quote from the documentation:
Primitive types and most of the data-types built-in in the JDK are serializable.
You can run into troubles only with these types:
Object
doesn't implementSerializable
, henceObject
type fields are not allowed in the class that is meant to be serializable.Optional
as well asOptionalInt
,OptionalLong
andOptianalDouble
aren't serializable because optional types were introduced as limited mechanismfor return types and not intended to be used as fields.
Tiles
class has to implementSerializable
and if there are some custom field types inside theTiles
, they must be serializable as well.If you don't want a particular field to be serialized, you can mark it as
transient
. Transient fields as well asstatic
fields are ignored during the serialization process.So if you would make the fields
tileGroup
andloadingTiles
to betransient
, thenTiles
class will not be required to implementSerializable
because these attributes will be excluded from the serialized version. And as a consiquence after deserialization they will be initialized tonull
.In general, serialization is meant to be used for temporary data-storage or transferring the data. To ensure that an object retrieved from memory is compatible with the .class file that you have it a good practice to define
static final long serialVersionUID
field, which denotes the current version of the class.Also, you should be aware that serialization has some serious draw-backs (take a look at the link provided above).
Sidenote: class-names are usually singular nouns like
Student
,Employee
,Tile
because their instances are meant to represent a single object.