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

时间:2017-02-01 14:51:05

标签: java getter-setter kryonet

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

错误:“JavaFX Application Thread”java.lang.IllegalArgumentException:object不能为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);
  }
 }

2 个答案:

答案 0 :(得分:2)

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

你需要决定自己的责任是什么?#34; 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);
});

答案 1 :(得分:-1)

丑陋但应该有效:

使ClientPlayer访问静态。在你的gui班:

public static ClientPlayer clientPlayer = new ClientPlayer();

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

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

编辑: 问题是,你没有引用"相同的"您在ClientController中通过clientPlayer对象,就像在GUI-Class中一样。你应该以某种方式将该对象交给或使用静态引用。您也可以在ClientController的构造函数中交出它。

EDIT2:

你应该怎么做

ClientPlayer clientPlayer = new ClientPlayer();  

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

在您的ClientController中:

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