我已经使用Java几个月了,我对它还是相当陌生,所以任何帮助都很感激。我一直致力于一款游戏(基于另一款游戏),并想要创造一个关卡编辑器。我在将信息保存到.ser文件时遇到了麻烦,尽管我在其他类的其他地方这样做没有问题。错误是java.io.NotSerializableException: resources.Sample$Ball
,但它给我的行号与这个类无关。我有一个叫做Sample的类,在这个类里面有一个叫做Ball的私有类,所以我明白了很多,但是它给我提供信息的事实是没有意义的。Sample
类只在单个类中使用一次,并且它不是我得到错误的那个。我从来没有遇到过这个类或Ball
子类的问题,因为它们非常隐蔽,很少使用。
错误给我的两个行号是:
at LevelEditor.saveDialog(LevelEditor.java:309)
at LevelEditor.actionPerformed(LevelEditor.java:256)
这些行号处的代码行如下:
oos.writeObject(new CustomSave(platforms, stars, ball, levelSize));
这是问题实际发生的地方,而另一行号正是我调用上述代码所在函数的地方。CustomSave
类接受的变量仍然与Sample
或Ball
类无关。因为CustomSave
课程很小,我也会在这里提供。我还将为上面的行号提供上下文。
上下文到代码
try {
File f = new File(filename);
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(new CustomSave(platforms, stars, ball, levelSize));
oos.close();
} catch (Exception e1) {
e1.printStackTrace();
}
<<p> CustomSave类/strong> package resources;
import java.awt.Dimension;
import java.io.*;
import java.util.ArrayList;
import resources.BouncyBall;
public class CustomSave implements Serializable {
private ArrayList<DragablePlatform> platforms;
private ArrayList<StarPolygon> stars;
private StaticBall ball;
private Dimension levelSize;
public CustomSave(ArrayList<DragablePlatform> platforms, ArrayList<StarPolygon> stars, StaticBall ball, Dimension levelSize){
this.platforms = platforms; this.stars = stars; this.ball = ball; this.levelSize = levelSize;
}
public ArrayList<DragablePlatform> getPlatforms() {
return platforms;
}
public ArrayList<StarPolygon> getStars() {
return stars;
}
public BouncyBall getBall() {
return ball;
}
public Dimension getLevelSize() {
return levelSize;
}
}
我不知道为什么我得到这个错误,因为它没有意义。为了尝试故障排除,我尝试在Sample
类中的Ball
类上实现Serializable
,并在一定程度上解决了问题。错误不再将Sample$Ball
列为问题,而是将另一个随机类列为问题,另一个类与我试图做的事情完全无关。
完整误差
java.io.NotSerializableException: resources.Sample$Ball
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeArray(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.access$300(Unknown Source)
at java.io.ObjectOutputStream$PutFieldImpl.writeFields(Unknown Source)
at java.io.ObjectOutputStream.writeFields(Unknown Source)
at java.awt.Container.writeObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at LevelEditor.saveDialog(LevelEditor.java:454)
at LevelEditor.actionPerformed(LevelEditor.java:298)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
关卡编辑器第454行
oos.writeObject(new CustomSave(platforms, stars, ball, levelSize));
关卡编辑器第298行
saveDialog();
(包含上面一行的函数)
所有的帮助都是非常感激的,如果你还需要什么就问我。
谢谢。
要正确序列化,你必须确保你要序列化的所有类都实现了Serializable接口,我曾经有过同样的问题,我试图一次保存一个对象,看看是什么对象引起了问题-冲洗。
try {
FileOutputStream fos = new FileOutputStream(new File(filename));
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(platforms);
oos.writeObject(stars);
oos.writeObject(balls);
oos.writeObject(levelSize);
oos.flush();
oos.close();
}catch (Exception e1) {
e1.printStackTrace();
}
要使序列化/反序列化成功,必须满足以下两个条件:
- 要序列化/反序列化的类必须实现
java.io.Serializable
接口。 - 类中的所有字段必须是基本类型或者必须实现
java.io.Serializable
接口。
条件1(满足):sinceCustomSave implements Serializable
Condition 2 (?): Do StaticBall
, DragablePlatform
and StarPolygon
实现Serializable?
private ArrayList<DragablePlatform> platforms; // ArrayList implements Serializable? Yes, DragablePlatform implements Serializable?
private ArrayList<StarPolygon> stars; // ArrayList implements Serializable? Yes, StartPolygon implements Serializable?
private StaticBall ball; // StaticBall implements Serializable?
private Dimension levelSize; // Dimension implements Serializable? Yes