当我尝试发送到服务器时,为什么我的 getter 返回 null?

Why are my getters returning null when I try to send to the server?

我知道这个问题在这里被问了无数次,我已经搜索了 SO 和其他资源来寻找解决方案,但我就是无法解决错误。我的 ClientPlayer class 属性有 getter 和 setter,调用某个按钮时会在 GUI 中调用 setter,我想使用 getter 客户端连接并将对象发送到服务器后。在 ClientController returns 中调用方法 "this.client.sendTCP(clientPlayer.getPlayerName());" 一个 nullPointerException。

错误:"JavaFX Application Thread" java.lang.IllegalArgumentException:对象不能为空。

我的猜测是,我创建了一个新的 ClientPlayer 实例,次数过多,因此返回 null,因为我的字符串 playerName 最初设置为 none。但是,我不确定如何解决这个问题。非常感谢任何帮助。

public class ClientPlayer implements Serializable {

public ClientPlayer() {

}

private String playerName;

public void setPlayerName(String playerName) {
    this.playerName = playerName;
   }

public String getPlayerName() {
    return this.playerName;
   }
}  

在我的 GUI 代码的相关部分下面,我设置了一个按钮来连接到服务器:

    ClientPlayer clientPlayer = new ClientPlayer();  

    clientToGame.setOnAction((ActionEvent w) -> {
         clientPlayer.setPlayerName(clientNameText.getText());
         clientController.connect();
         window.setScene(lobbyScene);
    });

这是我的客户端-class 我尝试获取名称并发送到服务器的部分:

    public class ClientController() {
     ClientPlayer clientPlayer = new ClientPlayer();

    public void connect() {
    if (client.isConnected()) {
        Logger.getLogger(getClass().getName()).log(Level.INFO, "You are      already connected to :{0}", config.getHost());
        return;
    }
    this.client.start();
    try {
        this.client.connect(5000, config.getHost(), config.getTCPPort());
        System.out.println("Successfully connected to " + config.getHost());
        this.client.sendTCP(clientPlayer.getPlayerName());

    } catch (IOException ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Server connection failed: {0}", ex.getMessage());
        throw new RuntimeException(ex);
    }

    MessageRegistry.registerMessages(client.getKryo());
    this.client.addListener(listener);
  }
 }

丑陋但应该工作:

使 ClientPlayer 访问静态。在你的图形界面 class:

public static ClientPlayer clientPlayer = new ClientPlayer();

然后您可以通过以下方式访问 ClientController 中的同一对象:

...
this.client.sendTCP(MyGuiClass.clientPlayer.getPlayerName());
...

编辑: 问题是,您不会像在 GUI-Class 中那样通过 ClientController 中的 clientPlayer 引用 "same" 对象。您应该以某种方式移交该对象或使用静态引用。你也可以在 ClientController.

的构造函数中传递它

编辑2:

你应该怎么做

ClientPlayer clientPlayer = new ClientPlayer();  

    clientToGame.setOnAction((ActionEvent w) -> {
         clientPlayer.setPlayerName(clientNameText.getText());
         clientController.connect(clientPlayer);
         window.setScene(lobbyScene);
    });

在您的客户端控制器中:

public void connect(ClientPlayer player) {
    this.clientPlayer = player;
    if (client.isConnected()) {
        Logger.getLogger(getClass().getName()).log(Level.INFO, "You are already connected to :{0}", config.getHost());
        return;
    }
    this.client.start();
    try {
        this.client.connect(5000, config.getHost(), config.getTCPPort());
        System.out.println("Successfully connected to " + config.getHost());
        this.client.sendTCP(player.getPlayerName());

    } catch (IOException ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Server connection failed: {0}", ex.getMessage());
        throw new RuntimeException(ex);
    }

    MessageRegistry.registerMessages(client.getKryo());
    this.client.addListener(listener);
}

在您的 GUI 中,您创建一个 ClientPlayer 实例并在其上调用 setPlayerName()。然后在 ClientController 中,您创建一个新的 ClientPlayer 实例(您永远不会对其调用 setPlayerName()),并对其调用 getPlayerName()。由于您从未为该实例设置玩家名称,getPlayerName() 当然 returns null.

您需要决定 "own" ClientPlayer 实例由谁负责。如果是 ClientController 的责任,那么要么在 ClientController 中添加一个 getClientPlayer() 方法,然后执行

clientToGame.setOnAction((ActionEvent w) -> {
     clientController.getClientPlayer().setPlayerName(clientNameText.getText());
     clientController.connect();
     window.setScene(lobbyScene);
});

并从您的 GUI class 中完全删除 ClientPlayer。如果 GUI class 有责任拥有它,则将对它的引用传递给 connect() 方法,并从控制器中删除 ClientPlayer 字段:

public class ClientController() {
     // ClientPlayer clientPlayer = new ClientPlayer();

    public void connect(ClientPlayer clientPlayer) {
    if (client.isConnected()) {
        Logger.getLogger(getClass().getName()).log(Level.INFO, "You are      already connected to :{0}", config.getHost());
        return;
    }
    this.client.start();
    try {
        this.client.connect(5000, config.getHost(), config.getTCPPort());
        System.out.println("Successfully connected to " + config.getHost());
        this.client.sendTCP(clientPlayer.getPlayerName());

    } catch (IOException ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Server connection failed: {0}", ex.getMessage());
        throw new RuntimeException(ex);
    }

    MessageRegistry.registerMessages(client.getKryo());
    this.client.addListener(listener);
  }
 }

当然还有

clientToGame.setOnAction((ActionEvent w) -> {
     clientPlayer.setPlayerName(clientNameText.getText());
     clientController.connect(clientPlayer);
     window.setScene(lobbyScene);
});