我已经使用 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));
问题实际出现在哪里,而另一行号正是我调用上面代码所在的函数的地方。该类接受的变量仍然与or类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();
}
自定义保存类
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;
}
}
同样,我不知道为什么会出现此错误,因为这毫无意义。为了尝试故障排除,我尝试Serializable
在Ball
班级内的Sample
班级上实施,并在一定程度上解决了问题。该错误不再将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();
(包含上述行的函数)
非常感谢所有帮助,如果您需要其他任何帮助,请询问。
谢谢你。