0

I am trying to transfer variable from one controller to another but i am not getting correct output:

My codes:

   public class CustomControl extends AnchorPane implements Initializable {
    String customId;

    public CustomControl() {
        //if you want to set a FXML
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/res/customControl.fxml"));
        //Defines this class as the controller
        fxmlLoader.setRoot(this);
        //this.getStylesheets().add("/res/style.css"); <- if you want to set a css
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

    }
        public String getCustomId() {
            return customId;
        }
    public void setCustomId(String customId) {
        return this.customId = customId;
    }
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
          //Initializes the controller
    }
}

To set CustomId variable at other controller

CustomControl c = new CustomControl();
c.setCustomId("StackOverflow");

To get CustomId variable from other controller

CustomControl c = new CustomControl();
c.getCustomId();
System.out.Println(c.getCustomId());

It gives me output

null

but required is

StackOverflow

And i know a same question is already asked Link So, don't mark it as duplicate

because In my question There are two controller at firstcontroller.java

 CustomControl c = new CustomControl();
    c.setCustomId("StackOverflow");

now at secondcontroller.java

CustomControl c = new CustomControl();
c.getCustomId();
  System.out.Println(c.getCustomId());

as we are getting as setting data in different controller so it gives me the output

null

please please please help me. Thank You.

Community
  • 1
  • 1
user3204934
  • 485
  • 6
  • 15
  • i had already told in my question that i know about this answer but this doesn't fit in my question – user3204934 Jun 13 '14 at 16:37
  • it doesn't help to post the exact same question again and again - as @James_D already suggested: if you don't understand the answer you'll have to learn some basics first. Closing this again as duplicate. Note that you can edit the question and clarify why you think and how exactly your problem differs from the answered one. Also, you might consider adding a SSCCE (short, standalone, runnable) example demonstrating the problem (please see the faq on how-to-ask for details) – kleopatra Jun 14 '14 at 07:37
  • i need help in my codes. – user3204934 Jun 14 '14 at 08:55

1 Answers1

1

In secondcontroller.java, you are instantiating a new object, c.

CustomControl c = new CustomControl();
c.getCustomId();
  System.out.Println(c.getCustomId());

This does not refer to the object of the same name in firstcontroller.java. You will need to pass the object c instantiated in firstcontroller.java to secondcontroller.java if you wish to access it there.

colti
  • 393
  • 1
  • 15