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

错误:“ JavaFX应用程序线程” java.lang.IllegalArgumentException:对象不能为null。

我的猜测是,我将创建一个新的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);
    });


这是我的客户端类的一部分,在这里我尝试获取名称并发送到服务器:

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

最佳答案

在您的GUI中,创建一个ClientPlayer实例并在其上调用setPlayerName()。然后在ClientController中,创建一个新的ClientPlayer实例(从不调用setPlayerName()),然后在其上调用getPlayerName()。由于您从未为该实例设置播放器名称,因此getPlayerName()当然会返回null。

您需要确定“拥有” ClientPlayer实例的责任。如果是ClientController的责任,则可以在getClientPlayer()中添加ClientController方法,然后执行

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


并从您的GUI类中完全删除ClientPlayer。如果拥有它,则是GUI类的责任,然后将对它的引用传递给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);
});

关于java - 尝试发送到服务器时,为什么我的 setter/getter 返回null?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41982694/

10-09 19:30