0

In a fairly simple program I wrote, I am saving an object (a game which contains a few other objects) using ObjectOutputStream. My first question is, when I remove "implements Serializable" from any of my classes, a NotSerializableException is NOT thrown. Why not? They are all extending Serializable classes, but shouldn't they themselves have to be Serializable as well?

Another problem I have, which may be related, is that when I read the object back in, I get a java.io.EOFException.

I don't understand why any of these two things are happening. I use the same exact file name for both reading and writing. Why is it hitting the end of the file before it's done?

Here's the writing code:

public void actionPerformed(ActionEvent event)
            {
                try
                {
                    saver.writeObject(game);
                    saver.close();
                } catch (IOException e)
                {
                    e.printStackTrace();
                }

                dispose();
            }

And here's the reading code:

File file = new File("savedgame.dat");
    if (file.exists())
    {
        try
        {
            loader = new ObjectInputStream(new FileInputStream(file));
            game = (GameBoard) loader.readObject();
            loader.close();
        }
        catch (EOFException ex)
        {
            ex.printStackTrace();
        }
    }
    else
    {
        game = new GameBoard();
    }

The exception is being thrown on the game = (GameBoard) loader.readObject(); line.

Here's the stack trace:

java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)

If it helps, I'm using many swing objects, but from my research, I'm pretty sure they're all serializable.

Thanks for the help!

6
  • Can you show readObject and writeObject of GameBoard? Commented Dec 20, 2012 at 1:27
  • @tcb I didn't override them. I have no idea how to do that. Would that help me serialize objects not referenced by instance variables? (see my comments on Thilo's answer) Commented Dec 20, 2012 at 1:34
  • Do you have a custom readObject method defined on the class you are trying to deserialize? Commented Dec 20, 2012 at 1:57
  • @Perception no, I didn't think I had to do that, and I don't know how to either. I don't think my idea of serializing the entire mastermind game is going to work, because from what I understand now, that would require every single object used in my classes to be stored as instance variable. I mean, if I add a layout manager to a JPanel, that's not going to be serialized :/ Commented Dec 20, 2012 at 2:03
  • @yts - actually, its not required for you to have a custom readMethod. But if you had one I would have asked you to include it in your question, as those can be a source of errors. Next question, how soon after writing the file data out do you try and read it back in? Commented Dec 20, 2012 at 2:10

2 Answers 2

1

They are all extending Serializable classes, but shouldn't they themselves have to be Serializable as well?

No. Serializable is a normal interface in Java, so you inherit it from superclasses, no need to declare it again. You are automatically considered serializable if you extend a serializable class. This is arguably a bit of a design problem in Java.

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

8 Comments

As for your IOException, can you verify that the file is not empty?
Is 4 bytes too small? Come to think of it, there are 2 images which should be being written, each of which are 10kb... It's being "modified" according to Windows every time my program writes the object. Does serialization only save objects referenced by instance variables, or once I add something to a JFrame is that essentially what's happening?
4 bytes is too small for a serialized object.
yeah, thought so. So why isn't it saving the game object (a JFrame with a bunch of JPanels, JLabels etc)?
I unserialized twp action listeners, and only the one which was referenced by an instance variable gave me a notserializableexception.. Does that mean that I have to have everything I want serialized referenced by instance variables?
|
1

My first question is, when I remove "implements Serializable" from any of my classes, a NotSerializableException is NOT thrown. Why not? They are all extending Serializable classes, but shouldn't they themselves have to be Serializable as well?

If the base class is Serializable, then derived class is also Serializable. This means you don't need to explicitly specify that your class implements Serializable.

1 Comment

Thanks, Thilo explained that to me. (no sarcasm intended!) My second question is what's really bothering me now :(

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.