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

Started by
10 comments, last by Aldacron 19 years, 4 months ago
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]
Advertisement
Where is the the NullPointerException occuring? Make sure you print out all your stack traces. Is it occuring in the load high scores or in another place?
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
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.
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.
-------------Golden T Game Engine - Java Game EngineGolden T Website | Golden T Forum
Is HighScore list serializable aswell? This could be the problem.
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.
-------------Golden T Game Engine - Java Game EngineGolden T Website | Golden T Forum
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?)
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?)
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 nulltry {    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
-------------Golden T Game Engine - Java Game EngineGolden T Website | Golden T Forum
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.

This topic is closed to new replies.

Advertisement