Sign in to follow this  
Kevinator

[java] Object Serialization - How Come my Object is Null?

Recommended Posts

So here's the relevant part of the code:
public class Game implements java.io.Serializable {

    private HighScoreList _highScoreList;

    public Game() {
        try {
	    java.io.FileInputStream fis = new java.io.FileInputStream("scores.hsl");
	    java.io.ObjectInputStream ois = new java.io.ObjectInputStream(fis);
	    _highScoreList = (HighScoreList) ois.readObject();
	    ois.close();
	}
	catch(Exception e) {

	}
    }
        //The high score list is updated as necessary here.
        
    public void saveHighScores() {
        try {
	    java.io.FileOutputStream fos = new java.io.FileOutputStream("scores.hsl");
	    java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(fos);
	    oos.writeObject(_highScoreList);
	    oos.close();
	}
	catch(Exception e) {
	}										
    }
}




The code assumes that a file named scores.hsl already exists which is a serialized _highScoreList object. What is wrong? Everytime I try to use a "loaded" high score object, I get a null pointer exception (the code compiles fine). Why isn't it setting the instance variable equal to the object in the file? Before any wiseguys ask, it was not null at the time it was serialized. I don't get it... This is really giving me a hard time. Thanks for any help. [Edited by - Kevinator on December 5, 2004 9:24:57 PM]

Share this post


Link to post
Share on other sites
The save and load routines appear to work flawlessly. The nullPointerException occurs whenever I try to use a method of _highScoreList, such as _highScoreList.displayHighScores(). The stack trace reveals nothing amazing, it just points to the line wherever the exception happens.

Share this post


Link to post
Share on other sites
Can't find anything wrong with your code.
It would be helpful if you write the exception here.

And try print out the _highScoreList object, is it null or not?
What I guess is perhaps not the _highScoreList is null, but some object in your _highScoreList that still null.
So (perhaps) by calling _highScoreList.displayHighScores(), it's calling a method of the null object inside the _highScoreList object.

Share this post


Link to post
Share on other sites
Nah, if the HighScoreList is not serializable, it would throwing exception when reading the object (deserialization).
He said the save and load routines appear to work flawlessly.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Not only that, but it is my understanding that all members of a class that declares itself as serializable are serializable as well by default. _highScoreList.displayHighScores() contains no references to any objects at all except the members of HighScoreList. Is it possible that the members of HighScoreList are not being serialized? (For your information - HighScoreList does not implement the serializable interface. Do I need to?)

Share this post


Link to post
Share on other sites
Not only that, but it is my understanding that all members of a class that declares itself as serializable are serialized as well by default. _highScoreList.displayHighScores() contains no references to any objects at all except the members of HighScoreList. Is it possible that the members of HighScoreList are not being serialized? (For your information - HighScoreList does not implement the serializable interface. Do I need to?)

Share this post


Link to post
Share on other sites
Huh? Of course the HighScoreList need to implement serializable interface, now that's really the problem.
Everything that want to serializable need to implement serializable, even all object inside the serializable thing need to implement serializable, or mark as transient.

But do you say the saving/loading is working?
I tried some test case, if the class is not implemented serializable, it will throw NotSerializableException.

Are you sure the saving/loading is working flawlessly?
Now look at your code, your saving/loading routine is catch the exception without print stack trace the exception.
If the HighScoreList not implementing serializable the saving/loading will throw exception.
No wonder the _highScoreList is still null.

_highScoreList;
// _highScoreList is null
try {
java.io.FileInputStream fis = new java.io.FileInputStream("scores.hsl");
java.io.ObjectInputStream ois = new java.io.ObjectInputStream(fis);
_highScoreList = (HighScoreList) ois.readObject(); // throwing NotSerializableException
ois.close();
} catch(Exception e) {
// you fell nothing happen cos you are not print out the exception
// e.printStackTrace();
}
// the _highScoreList is still null here cos the loading is not working

_highScoreList.displayHighScores(); // calling method from NULL object, throw NullPointerException

Share this post


Link to post
Share on other sites
I understand now, thank you. And curses to the book that made it sounds as if I only needed my main class to implement the serializable interface. ;) As for seemingly errorless routines, let's just say I really neglect my try-catch blocks and exception handling. ;) Thanks again.

Share this post


Link to post
Share on other sites
So now it work isn't it? :-)
And you don't need the main class (Game class) to implement the serializable, but the HighScoreList and all class inside it that must implement serializable.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kevinator
I understand now, thank you. And curses to the book that made it sounds as if I only needed my main class to implement the serializable interface. ;)


Likely you just misunderstood it. If a class implements Serializable, subclasses of that class are Serializable also. You must have interpreted the term base class the wrong way. Member objects will be serialized only if they implement the Serilizable interface.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this