0

I am a beginner and I've complete the project. Now, I successfully serialize the objects in the ArrayList, and when opening the program again, I want the program to load the data.

However, when I deserialize, I use another ArrayList and add the objects first. Then, set it to the original one. It failed. I can no longer use my methods to call them.

I am sure that I serialize the object properly that I can call them using the getters in the NimPlayer when deserializing.

How I serialize and deserialize:

private void saveData() {
    ArrayList<NimPlayer> playerList = nimModel.getPlayerList();
    if (playerList.size() != 0) {
        try {
            FileOutputStream fileOut =
            new FileOutputStream("players.dat.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(playerList);
            out.close();
            fileOut.close();
            System.out.printf("Serialized data is saved in players.dat.ser");
         } catch (IOException i) {
            i.printStackTrace();
         }

    } else {
        System.out.println("There is no data to be stored.");
    } 
}

 private void loadData(String fileName) {
 NimModel nimModel = new NimModel();
    try {
        FileInputStream fis = new FileInputStream(fileName);
        ObjectInputStream ois = new ObjectInputStream(fis);
        ArrayList<NimPlayer> newPlayerList = (ArrayList<NimPlayer>)ois.readObject();
        nimModel.setPlayerList(playerList);// I THOUGHT IT WOULD WORK

     } catch (IOException i) {
        i.printStackTrace();
        return;
     } 
}

Here is part of my NimPlayer class

public class NimPlayer implements Serializable  {

private final String userName;
private String familyName;
private String givenName;

private int gamesPlayed;
private int gamesWon;

public NimPlayer(String userName, String familyName, String givenName) {

    this.userName = userName;
    this.familyName = familyName;
    this.givenName = givenName;
    this.gamesPlayed = 0;
    this.gamesWon = 0;
}
//getter and setters
}

And the getters and setters of ArrayList is in the NimModel:

public class NimModel {

private ArrayList<NimPlayer> playerList = new ArrayList<NimPlayer>();

public ArrayList<NimPlayer> getPlayerList() {
    return new ArrayList<NimPlayer>(playerList);
}

public void setPlayerList(ArrayList<NimPlayer> playerList) {
    this.playerList = playerList;
}
public void displayAllPlayers() {
    Collections.sort(playerList, (NimPlayer p1, NimPlayer p2) -> {
        return p1.getUserName().compareToIgnoreCase(p2.getUserName());
    });

    if (playerList.size() != 0) {
        for (NimPlayer player : playerList) {
            String userName = player.getUserName();
            String familyName = player.getFamilyName();
            String givenName= player.getGivenName();
            int gamesWon = player.getGamesWon();
            int gamesPlayed = player.getGamesPlayed();

            System.out.println(userName + "," + familyName + "," + givenName + "," + gamesPlayed + " games," + gamesWon + " wins");
        }
    } else {
        System.out.println("There is no players.");
    }

}
}

Any help is highly appreciated

reference

Getters and Setters for ArrayLists in Java

Using Getter and Setter for arraylist in java

Getters and Setters for ArrayLists in Java

Woden
  • 1,054
  • 2
  • 13
  • 26
  • What exactly is happening? Are you getting a compilation error, if so which is the error? Or is it a runtime exception, if so which one is it? – fpezzini May 11 '20 at 10:06
  • Error-free, though. I thought I've cleared every noise, but the functions I've created such as `displayplayer` cannot call the ArrayList when I set them. @fpezzini – Woden May 11 '20 at 10:09
  • 1
    Ok, please add the code you're using to serialize the data and also this method displayPlayer so we can have a look at what can be going wrong. Also point out which line of code is causing you trouble. – fpezzini May 11 '20 at 10:13
  • thank you @fpezzini I'll add them in. – Woden May 11 '20 at 10:16
  • 1
    You are creating a local variable `NimModel nimModel = new NimModel();` inside your `loadData` method and set the data on that new local variable. Local variables are only available and alive inside the block that they are declared. You need to use your already existing object of NimModel and set the list there. What class did you declare your save and load methods in? – OH GOD SPIDERS May 11 '20 at 11:07
  • I declare the save and load methods in the main class. When I open the program, the `loadData()` will execute. @OHGODSPIDERS – Woden May 11 '20 at 11:13
  • 1
    You either need to pass your existing instance of NimModel into the load method and set the List on that passed variable, or you could make the load and save methods part of the NimModel class so that you can call them directly on the existing instance. – OH GOD SPIDERS May 11 '20 at 11:19
  • I tried to put both the `saveData` and `loadData` in my `NimModel` class. When I load, the `NullPointerException` shows up. Do I miss anything? @OHGODSPIDERS – Woden May 11 '20 at 11:30
  • 1
    [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – OH GOD SPIDERS May 11 '20 at 11:33
  • Thank you @OHGODSPIDERS I've fixed the issue. Have a nice day. – Woden May 11 '20 at 11:48

0 Answers0