0

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

}

12
  • 1
    can you add in the code for your Block class? Commented Feb 20, 2014 at 21:36
  • try declaring list you're reading, as Block[] Commented Feb 20, 2014 at 21:40
  • 2
    Comment only - You should use serialVersionUID's in your serializable classes Commented Feb 20, 2014 at 21:43
  • Can we see the delcaration of your list variable? Commented Feb 20, 2014 at 21:45
  • 1
    You're not far from providing a complete, reproducible program. please do so by providing the complete code of your three classes : BlockList, Block and RGB, and a main method trying to save and load the list from a file. Commented Feb 20, 2014 at 21:58

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

8 Comments

I'm not exactly sure how am I supposed to do that.
Delete the .bk file, have saveToHardDrive() be invoked, then try loadFromHardDrive() again
How could the OP create a file containing a serialized List<Block> if Block wasn't serializable?
Well thought, that was it... Can someone please explain what happened ?
thats what I was wondering JB, but maybe EJP knows something I dont with his 93k rep :P
|
0

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

2 Comments

"I mean, all my classes possibly contained in the list are Serializable. Why do I even get this error ?" I believe OP already addressed this.
No problem. Easy mistake to make. OP's terminology doesn't make it instantly obvious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.