通过构造函数在ArrayList中添加对象

I am a beginner, and I am building a classical NimGame. Before, I used to save the project using an array. Now, I modify it to apply the ArrayList to this time. It seems no problem, though, the functions I've made are not working without any errors. I couldn't figure out why.

For now, I tried to add the NimPlayer type into the new playerList, which is the ArrayList. I put the ArrayList in the NimModel, and use the constructor from the NimPlayer to create new players. The Nimsys is the main panel to give commands and receive user inputs. That's why I separate them into three classes.

The command is like this $addplayer userName,familyName,givenName. And the scanner should process the string and go through the constructor to be a new object.

非常感谢您的帮助,感谢您的友好和耐心。

Here is my related code Nimsys:

public class Nimsys {

private NimModel nimModel;

public static void main(String[] args) {
    Nimsys nimsys = new Nimsys();
    nimsys.processCommands();
}

private void processCommands() {
    this.nimModel = new NimModel();
    Scanner in = new Scanner(System.in);

    System.out.println("Welcome to Nim\n");
    while (true) {
        System.out.print('$');
        String commandin = in.nextLine().trim();

        if (commandin.equalsIgnoreCase("addplayer")) {
            addplayer(in);
        }
        if (commandin.equalsIgnoreCase("removeplayer")) {
            removeplayer(in);
        }
            private void addplayer(Scanner in) {
    String inName = in.nextLine().trim();
    String[] name = splitName(inName);

    if (name != null && name.length == 3) {
        ArrayList<NimPlayer> playerList = nimModel.getPlayerList();
        for (NimPlayer player: playerList) {
            if (player.getUserName().contains(name[0])) {
                System.out.println("The player already exists.");
                return;
            } else {
                nimModel.createPlayer(name[0], name[1], name[2]);
                System.out.println("The player has been created.");
            }
        }        
    } 
}

private String[] splitName(String inName) {
    String[] splittedLine = inName.split(",");
    String[] name = null;
    if (splittedLine.length == 3) {
        String userName = splittedLine[0].trim();
        String familyName = splittedLine[1].trim();
        String givenName = splittedLine[2].trim();
        name = new String[3];
        name[0] = userName;
        name[1] = familyName;
        name[2] = givenName;
    }
    return name;
}

private void removeplayer(Scanner in) {
    String removeUserName = in.nextLine().trim();
    NimPlayer player = nimModel.removePlayer(removeUserName);
    if (player == null) {
        System.out.println("The player does not exist");
    } else {
        System.out.println("Player " + player.getUserName() + 
                " removed successfully!");
    }
}

And the NimModel:

public class NimModel {

private NimPlayer nimplayer;

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

public void createPlayer(String userName, String familyName, String givenName) {
    NimPlayer player = new NimPlayer(userName, familyName, givenName);
    playerList.add(player);     

}

public ArrayList<NimPlayer> getPlayerList() {
    return playerList;
}
public NimPlayer removePlayer(String userName) {
    for (NimPlayer player: playerList) {
        String nameCheck = nimplayer.getUserName();
        String playerName = player.getUserName();
        if (playerName.equals(nameCheck)) {
            playerList.remove(player);
            break;
        } 
    }
    return null;

Lastly, NimPlayer class

public class NimPlayer {


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

private int gamesPlayed;
private int gamesWon;
private int winRatio;



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

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