F. P.
F. P.

Reputation: 5086

Can't serialize an ArrayList

This is a followup to Serializing a vector

I'm trying to implement loading and saving for a game I'm working on.

I want to save a Maze which contains, among other attributes, an ArrayList of Entity.

Entity is a super class for Dragon, Hero and Item. All three of these types can be contained at once in the vector.

Using the "automatic" serialization mechanism (adding implements Serializable to Maze) saves all attributes but the ArrayList.

Why does this happen?

My code is below, for completeness' sake.

package logic;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public final class LoadAndSave {
    public static final transient boolean available = false;

    public static final boolean serialize(Object obj) {

        // Write to disk with FileOutputStream
        FileOutputStream saveFile;
        try {
            saveFile = new FileOutputStream("game.sav");
        } catch (FileNotFoundException e) {
            return false;
        }

        // Write object with ObjectOutputStream
        ObjectOutputStream objOut;
        try {
            objOut = new ObjectOutputStream(saveFile);
        } catch (IOException e) {
            //
            return false;
        }

        // Write object out to disk
        try {
            objOut.writeObject(obj);
        } catch (IOException e) {
            return false;
        }

        return true;
    }

    public static final Object load() {
        FileInputStream fileIn;
        try {
            fileIn = new FileInputStream("game.sav");
        } catch (FileNotFoundException e1) {
            return null;
        }

        // Read object using ObjectInputStream
        ObjectInputStream objIn;
        try {
            objIn = new ObjectInputStream(fileIn);
        } catch (IOException e) {
            return null;
        }

        // Read an object
        Object obj;
        try {
            obj = objIn.readObject();
        } catch (IOException e) {
            return null;
        } catch (ClassNotFoundException e) {
            return null;
        }

        return obj;
    }

}

Upvotes: 1

Views: 4621

Answers (3)

Russ Hayward
Russ Hayward

Reputation: 5667

You need to make sure that you close your streams. Your code for load() can be simplified to look something like this:

ObjectInputStream objIn = null;
try {
    objIn = new ObjectInputStream(new FileInputStream("game.sav"));
    return objIn.readObject();
} catch (Exception exception) {
    // Deal with errors
    return null;
} finally {
    if (objIn != null) {
        try {
            objIn.close();
        } catch (Exception exception) {}
    }
}

serialise() can be altered in a similar way.

Upvotes: 1

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340708

I checked Your code with the following assumptions:

class Entity implements Serializable {}

and:

private char[][] mazeWalls = new char[0][0];

private Vector<Entity> entities = new Vector<Entity>();

...and it works perfectly, serializing and deserializing empty Entity objects...

Upvotes: 1

Augusto
Augusto

Reputation: 29827

Is Entity serializable too? (you mentioned that just Maze is serializable).

And be sure that the list is not defined as transient or static, otherwise it will be skipped by the serialization mechanism.

Upvotes: 7

Related Questions