gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:57:03 24 4
gpt4 key购买 nike

我知道这个问题已经在这里被问过无数次了,我已经搜索了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);
});

并删除 ClientPlayer完全来自你的 GUI 类。如果 GUI 类有责任拥有它,则将对它的引用传递给 connect()方法,并删除 ClientPlayer来自 Controller 的字段:

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 - 当我尝试发送到服务器时,为什么我的 getter 返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41982694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com