import javax.swing.JOptionPane;
public class connectFenetre {
private String host ;
private Integer port ;
public connectFenetre(){
}
public void run()
{
String input = JOptionPane.showInputDialog("What's the port? and host?
String[] token = input.split(":");
if(token.length != 2){
JOptionPane.showMessageDialog(null, "need host and port!");
}
this.host = token[0];
this.port = Integer.parseInt(token[1]);
System.out.println(getPort());
System.out.println(getHost());
CommBase c = new CommBase();
c.start();
FenetrePrincipale fenetre = new FenetrePrincipale(c);
c.setPropertyChangeListener(fenetre);
}
public String getHost(){
return host;
}
public Integer getPort(){
return port;
}
}
为什么我的端口和主机在其他班级中返回null?
在其他类中我声明
connectFenetre c = new connectFenetre();
println("c.getPort()");
println("c.getHost()");
Output: NULL NULL
为设置要执行的主机和端口的代码创建c
之后,您需要调用c.Run()
。尝试以下操作:
connectFenetre c = new connectFenetre();
c.run();
println("c.getPort()");
println("c.getHost()");
您可能还需要将主机和端口字段初始化为connectFenetre
构造函数中的某些默认值。即使从未执行run()
,这也将避免NULL
值。