Search code examples
javaexceptionserializationlibrariesnotserializableexception

NotSerializableException in Third-Party Graphics Library


I have created a clone of Atari Breakout game using the ACM graphics library and just finished adding a highscore interface and functionality. The player's name and score should be displayed on the GUI window (it is successfully) and also be written to a .dat binary file.

However, when the code attempts to load the existing file I get the following error.

writing aborted; java.io.NotSerializableException: acm.graphics.GCanvasListener

I've researched this error online and it seems it can be solved by editing the class to implement Serializable. However, the class throwing this error is not one of my own but rather a class that belongs to the third-party ACM graphics library. How do I solve this?

I'm not even sure why this error is being caused in the first place since the data I'm attempting to serialize is only a name and score, I'm not trying to serialize a canvas of objects or anything like that.

Main class (called Breakout)

public class Breakout extends GraphicsProgram {
    ... // game variables
    public void run() {
        ... // this if clause runs when game ends
        if (brickCounter > 0) {
                removeAll(); // clears screen
                printGameOver(); // displays game over message
                HighscoreManager hm = new HighscoreManager();
                String name = getHighScoreName();
                hm.addScore(name, score);
                hm.displayHighscores();
        }
    }
    ... // game functionality methods
    private String getHighScoreName(){
        IODialog dialog = new IODialog();
        String name = dialog.readLine("Enter your name: ");
        return name;
    }

Score class

private class Score implements Serializable {
    private int score;
    private String name;

    public Score(String name, int score) {
        this.score = score;
        this.name = name;
    }

    public int getScore() { return score; }
    public String getName() { return name; }
}

ScoreComparator class

private class ScoreComparator implements Comparator<Score> {
    public int compare(Score score1, Score score2) {

        int sc1 = score1.getScore();
        int sc2 = score2.getScore();

        if (sc1 > sc2) {
            return -1;
        } else if (sc1 < sc2) {
            return 1;
        } else {
            return 0;
        }
    }
}

HighscoreManager class

private class HighscoreManager {
    private ArrayList<Score> scores;
    private static final String HIGHSCORE_FILE = ".//bin//scores.dat";
    ObjectOutputStream outputStream = null;
    ObjectInputStream inputStream = null;

    public HighscoreManager() {
        scores = new ArrayList<Score>(10);
    }

    public ArrayList<Score> getScores() {
        loadScoreFile();
        sort();
        return scores;
    }

    private void sort() {
        ScoreComparator comparator = new ScoreComparator();
        Collections.sort(scores, comparator);
    }

    public void addScore(String name, int score) {
        loadScoreFile();
        scores.add(new Score(name, score));
        updateScoreFile();
    }

    public void loadScoreFile() {
        try {
            inputStream = new ObjectInputStream(new FileInputStream(HIGHSCORE_FILE));
            scores = (ArrayList<Score>) inputStream.readObject();
        }
        catch (FileNotFoundException e) {
            System.out.println("[Load] File Not Found Error: " + e.getMessage());
        }
        catch (IOException e) {
            System.out.println("[Load] Input/Output Error: " + e.getMessage());
        }
        catch (ClassNotFoundException e) {
            throw new RuntimeException("[Load] Class Not Found Error: " + e.getMessage());
        }
        finally {
            try {
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (IOException e) {
                System.out.println("[Load] Input/Output Error: " + e.getMessage());
            }
        }
    }

    public void updateScoreFile() {
        try {
            outputStream = new ObjectOutputStream(new FileOutputStream(HIGHSCORE_FILE));
            outputStream.writeObject(scores);
        }
        catch (FileNotFoundException e) {
            System.out.println("[Update] File Not Found Error: " + e.getMessage());
        }
        catch (IOException e) {
            System.out.println("[Update] Input/Output Error: " + e.getMessage());
        }
        finally {
            try {
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (IOException e) {
                System.out.println("[Update] Input/Output Error: " + e.getMessage());
            }
        }
    }

    public void displayHighscores() {
        int max = 10;
        ArrayList<Score> scores;
        scores = getScores();
        int x = scores.size();

        if (x > max) {
            x = max;
        }

        removeAll(); // clears screen
        int npos = 160;
        int spos = 160;

        for (int i = 0; i < x; i++) {
            GLabel showName = new GLabel(scores.get(i).getName(), (getWidth() / 2.0) - 100, (getHeight() / 2.0) - npos);
            showName.move(-showName.getWidth() / 2, -showName.getHeight());
            showName.setColor(Color.WHITE);
            add(showName);
            npos -= 40;
        }

        for (int i = 0; i < x; i++) {
            GLabel showScore = new GLabel(Integer.toString(scores.get(i).getScore()), (getWidth() / 2.0) + 100, (getHeight() / 2.0) - spos);
            showScore.move(-showScore.getWidth() / 2, -showScore.getHeight());
            showScore.setColor(Color.WHITE);
            add(showScore);
            spos -= 40;
        }
    }

After running the application:

[Load] Input/Output Error: writing aborted; java.io.NotSerializableException: acm.graphics.GCanvasListener
[Update] Input/Output Error: acm.graphics.GCanvasListener
[Load] Input/Output Error: writing aborted; java.io.NotSerializableException: acm.graphics.GCanvasListener

Solution

  • Your task will be to find a hidden reference from your name and score structure to the UI components. Many GUI applications use a lot of inner classes, and this might be the missing link.

    When you have a class something like this:

    class MyGame {
        private SomeUIWidget widget;
        class TopScore implements Serializable {
            String name;
            int score;
            ...
        }
        ...
    }
    

    There is a hidden member in TopScore that references the "enclosing instance" of MyGame, including its SomeUIWidget member. When you try to serialize a TopScore instance, all the rest gets dragged in with it.

    You could simply declare TopScore as a static nested class. This means that there is no enclosing instance, and serves only to hide the TopScore class from other code. But, I would suggest just making TopScore a top-level class, in its own file, because it's likely that other objects will want to use those objects in different ways—that is, it seems like a likely candidate for part of your public API.

    This is an educated guess, in the absence of any actual code. To get a better answer, reduce your code to the minimum required to demonstrate the problem, and include that in your question.