gpt4 book ai didi

Java - NotSerializedException

转载 作者:行者123 更新时间:2023-12-01 23:11:21 24 4
gpt4 key购买 nike

我现在正把头撞到墙上。

事情是这样的:我创建了一个 BlockList 类,在其中创建 block 列表。这个类是序列化的,Block 类也是序列化的(也是我在 Block 类中使用的 RGB 类)。

因此,当我尝试使用 ObjectOutputStream 将列表保存在二进制文件中时,一切正常。

但是当我尝试读取该文件时,我收到此 NotSerialized 异常。我的意思是,列表中可能包含的所有类都是可序列化的。为什么我会收到此错误?

public void saveToHardDrive(){
try{
FileOutputStream fos = new FileOutputStream("blocks.bk", true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.close();
}catch(IOException e){
System.out.println("ERREUR : erreur lors de la sauvegarde.");
}
}

public void loadFromHardDrive(){
try {
FileInputStream fis = new FileInputStream("blocks.bk");
ObjectInputStream ois = new ObjectInputStream(fis);
list = (ArrayList<Block>) ois.readObject();
ois.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {

e.printStackTrace();
}
}

如果有人能找到,我就欠你一辈子了。

PS:更奇怪的是:当我尝试加载文件时,错误还提到了保存功能...即使我注释了所有功能。

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: vector.Block
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.util.ArrayList.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at vector.BlockList.loadFromHardDrive(BlockList.java:60)
at vector.main.file(main.java:184)
at vector.Controls.Controls(Controls.java:57)
at vector.main.gameLoop(main.java:111)
at vector.main.main(main.java:171)

Caused by: java.io.NotSerializableException: vector.Block
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.ArrayList.writeObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.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.writeObject(Unknown Source)
at vector.BlockList.saveToHardDrive(BlockList.java:51)
at vector.main.file(main.java:189)
at vector.Controls.Controls(Controls.java:57)
at vector.main.gameLoop(main.java:113)
at vector.main.main(main.java:173)

编辑:这是 Block 类:

public class Block implements Serializable{
private float x;
private float y;
private float height;
private float length;
private RGB color;

public Block(float x, float y){
this.x = x;
this.y = y;
length = 100;
height = 100;
color = new RGB(1, 1, 1);
}

public Block(Player p){
setX(p.getX());
setY(p.getY());
setHeight(p.getHeight());
setLength(p.getLength());
setColor(new RGB(1, 1, 1));
}

public void render() {
Quad.renderQuad(this);
}

还有 RGB 以防万一:

public class RGB implements Serializable{
float r, g, b;

public RGB(float R, float G, float B){
r=R;g=G;b=B;
}
public String toString(){
return (r*250) + ", " + (g*250) + ", " + (b*250);
}

}

最佳答案

您在使 vector.Block 可序列化之前创建了该文件。您必须重新创建它。

关于Java - NotSerializedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21920082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com