-1

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!

c0der
  • 18,467
  • 6
  • 33
  • 65
  • 1
    If any other class needs to use the the information in object p1 (`p1.getName();`) it needs to get a reference to `p1`. The code posted is not [mcve] and does not reproduce the problem you ask about. – c0der Dec 08 '18 at 06:40
  • So for example, if i create another class alongside these two, lets say "example.java", and do "System.out.println(p1.getName());", to my understanding it should fetch the value of whatever we set the Name of "P1" to in the welcome controller class, however, instead of this it returns null. – EshanMistry73 Dec 08 '18 at 06:44

1 Answers1

0

For any other class to use Player object p1 it need to get a reference to it.
If you want to use p1 in Example.java, as you asked you would need to pass a reference of p1 to it. You could do it in number of ways, constructor being one :

 new Example(p1);

Seter would be another

 Example ex = new Example();
 ex.setPlayer(p1); 

To demonstrate passing a reference in your use case use

main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.StackPane?>

<StackPane xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" 
                                                            fx:controller="src.tests.xml.MainController">
   <children>
      <Label fx:id="nameLabel" text="Label" />
   </children>
</StackPane>

MainController.java

import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class MainController {

    @FXML
    Label nameLabel;

    void setPlayerName(String name){
        nameLabel.setText(name);
    }
}

and edit pressButton:

@FXML
private Button btn_start;
@FXML
private TextField field_player;

@FXML
public void pressButton(ActionEvent event){

    Player p1 = new Player();
    p1.setName(field_player.getText());

    try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("main.fxml"));
            Parent root = (Parent) fxmlLoader.load();
            MainController mainController = (MainController)fxmlLoader.getController();
            mainController.setPlayerName(p1.getName()); //pass a reference of name to the other controller 
            Stage stage = new Stage();
            stage.setScene(new Scene(root));
            stage.show();
    } catch(Exception e) {
       e.printStackTrace();
    }
 }
c0der
  • 18,467
  • 6
  • 33
  • 65
  • Hi! Thank you for the informative post, much appreciated! I created Example.java just to test, and when i try the first two methods, i get an error simply telling me to create a local variable/field of "p1", which if i try either then run it still returns "null", – EshanMistry73 Dec 08 '18 at 07:53
  • The second method does work and i can see that the variable then shows in the other controller, however, how do i use "p1" in other classes, such as if i wanted to System.out.println(p1) in another class, lets say Example.java again, and display the name that was inputted? – EshanMistry73 Dec 08 '18 at 07:54
  • Sorry I can't answer. I have no idea what do you mean by `the first two methods` or 'other controller`. For questions about `Example` please have a new post and include mcve. For the original question use The code I posted. – c0der Dec 08 '18 at 08:20