-1

我已经使用 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无关。而且因为班级太小,我也会在这里提供。我还将为上面的行号提供上下文。SampleBallCustomSave

代码上下文

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;
    }
}

同样,我不知道为什么会出现此错误,因为这毫无意义。为了尝试故障排除,我尝试SerializableBall班级内的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();(包含上述行的函数)

非常感谢所有帮助,如果您需要其他任何帮助,请询问。

谢谢你。

4

2 回答 2

1

要使序列化/反序列化成功,必须满足以下两个条件:

  1. 您要序列化/反序列化的类必须实现该 java.io.Serializable 接口。
  2. 该类中的所有字段必须是原始类型或必须实现 java.io.Serializable接口。

条件1(满足):因为 CustomSave implements Serializable

条件 2 (?):执行StaticBallDragablePlatform实现StarPolygonSerializable?

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
于 2016-08-16T02:54:30.243 回答
1

要正确进行序列化,您必须确保要序列化的所有类都实现 Serializable 接口,一旦我尝试一次保存一个对象并查看是什么对象导致了问题,我遇到了同样的问题 - 刷新 oos。

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();
}
于 2016-08-16T01:51:46.913 回答