首页 > 解决方案 > 第三方图形库中的 NotSerializableException

问题描述

我使用ACM 图形库创建了 Atari Breakout 游戏的克隆,并刚刚添加了高分界面和功能。玩家的名字和分数应该会显示在 GUI 窗口上(它是成功的),并且会被写入一个.dat二进制文件。

但是,当代码尝试加载现有文件时,出现以下错误。

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

我在网上研究了这个错误,似乎可以通过编辑类来Serializable解决它。但是,抛出此错误的类不是我自己的,而是属于第三方 ACM 图形库的类。我该如何解决这个问题?

我什至不确定为什么首先会导致此错误,因为我尝试序列化的数据只是一个名称和分数,我没有尝试序列化对象画布或类似的东西。

主类(称为 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;
    }

成绩等级

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; }
}

分数比较器类

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 类

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;
        }
    }

运行应用程序后:

[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

标签: javaexceptionserializationlibrariesnotserializableexception

解决方案


您应该转到字段名称分数所在的类,并添加例如public class nameclass implements Serializable. 我希望这个对你有用。


推荐阅读