سؤال

I am hitting my head on the walls right now.

Here's the deal : I have made a BlockList class where I create a list of blocks. This class is Serialized and so is the Block class (And also is the class RGB I use in the Block class).

So when I try to save the list in a binary file with ObjectOutputStream, everything works just fine.

But when I try to read the file, I get this NotSerializable exception. I mean, all my classes possibly contained in the list are Serializable. Why do I even get this error ?

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

If anyone can find, I owe you for the rest of my life.

PS: even weirder : When I try to load the file, the error also mentions the save function... Even though I comment all the function.

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)

Edit : Here's the Block class :

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

And RGB just in case :

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

}

هل كانت مفيدة؟

المحلول

You created the file before you made vector.Block Serializable. You have to recreate it.

نصائح أخرى

All classes that should be serialized, must implement the Serializable-Interface.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top