Learning JavaFX, i have two textfields where the user can enter two player names, which i want to store to use for the program (its a game) and use the names in the other classes, including the maingame class. If i print in the welcomecontroller, it prints the value entered fine when you click submit, however, in any other class, if i do p1.getName(); it returns "null"
Ive made a player class with setters and getters to set the name of the object, ive included the code below! Any help would be greatly appreciated!
welcome controller
@FXML
private Button btn_start;
@FXML
public void pressButton(ActionEvent event){
//Create and Set Player1 Name
player p1 = new player();
p1.setName(field_player1.getText());
//Create and Set Player2 Name
player p2 = new player();
p2.setName(field_player2.getText());
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("main.fxml"));
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
player.java
public class player {
public String Name;
public void Player(String Name) {
this.Name = Name;
}
public String Name() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getName() {
return this.Name;
}
}
So in any other class apart from the welcome controller, whenever i do p1.getName(); it returns null
In the welcome controller if i do p1.getName(); it will return whatever is entered in textfield1.
Thanks in advance!