0

I'm stuck trying to deserialize a list of Scores. I spent my entire day searching here but couldn't find a solution.. My code looks something like this:

public class Score implements Comparable<Score>, Serializable {
    private String name;
    private int score;
    // .......

}

public class MySortedList<T> extends...implements...,Serializable {

    private ArrayList<T> list;
    // ....
}

public class ScoreManager {

    private final String FILEPATH;
    private final String FILENAME = "highscores.ser";

    private MySortedList<Score> scoreList;

    public ScoreManager() {

        File workingFolder = new File("src\\games\\serialized");
        if (!workingFolder.exists()) {
            workingFolder.mkdir();
        }
        FILEPATH = workingFolder.getAbsolutePath();

        if ((scoreList = loadScores()) == null) {
            scoreList = new MySortedList<Score>();
        }
    } 

    public void addScore(Score score) {
        scoreList.add(score);
        saveScores();
    }

    public MySortedList<Score> getScoreList() {
        return scoreList;
    }

   private void saveScores() {
       try (ObjectOutputStream out = new ObjectOutputStream(new   FileOutputStream(new File(FILEPATH, FILENAME)))) {
        out.writeObject(scoreList);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
 }

@SuppressWarnings("unchecked")
private MySortedList<Score> loadScores() {
    try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(FILEPATH, FILENAME)))) {
        return (MySortedList<Score>) in.readObject();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
}

The loadScores() method returns just an empty MySortedList everytime. However program succesfully creates the highscores.ser file in the correct place and I have absolutely no errors. Score objects are added correctly to the MySortedList object.

Any ideas? Perhaps worth mentioning that this is a part of a bigger program made in Swing. the methods in the ScoreManager class is called when the player dies

A. Lindberg
  • 299
  • 4
  • 13

1 Answers1

-1

only if it can help, this code is working for me:

class Score implements Comparable<Score>, Serializable{
    private int point;
    public Score(int point) {
        this.point = point;
    }
    public int getPoint(){
        return point;
    }
    @Override
    public int compareTo(Score o) {
        if (o.getPoint() == this.getPoint()) 
            return 0;
        return this.point < o.getPoint() ? - 1 : 1;
    }
    public String toString() {
        return "points: " + point;
    }

}
class MyList<T> implements Serializable {
    private ArrayList<T> list = new ArrayList<>();
    public void add(T e){
        list.add(e);
    }
    public void show() {
        System.out.println(list);
    }
}

public class Main {
    File workingFolder;
    String FILEPATH;
    private final String FILENAME = "highscores.ser";


    MyList<Score> list = new MyList<>();

    public static void main(String[] args) {

        Main main = new Main();
        main.createFolder();
        main.addItems();
        main.saveScores();
        MyList<Score> tempList = main.loadScores();
        tempList.show();
        main.addMoreItems();
        main.saveScores();
        tempList = main.loadScores();
        tempList.show();
    }
    private void addItems() {
        Score sc = new Score(10);
        list.add(sc);
    }
    private void addMoreItems() {
        Score sc1 = new Score(20);
        list.add(sc1);
        Score sc2 = new Score(30);
        list.add(sc2);
    }
    private void createFolder() {
        workingFolder = new File("src\\games\\serialized");
        if (!workingFolder.exists()) {
            workingFolder.mkdir();
        }
        FILEPATH = workingFolder.getAbsolutePath();
    }

    private void saveScores() {
        System.out.println("before save: " + list);
           try (ObjectOutputStream out = new ObjectOutputStream(new   FileOutputStream(new File(FILEPATH, FILENAME)))) {
            out.writeObject(list);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    private MyList<Score> loadScores() {
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(FILEPATH, FILENAME)))) {
            return (MyList<Score>) in.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
OscarBcn
  • 626
  • 4
  • 11