-1

I am trying to transfer an object from Controller A to Controller B, but whenever I try, it sets my object as null. I have an object of called user in Controller B. Also in Controller B I have a method called public void setUser(). The full method is: public void setUser(User user) { this.user = user; In Controller A, I do the following:

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("B.fxml"));
loader.load();
ControllerB ctrl = loader.getController();
cntrl.setUser(user); 

In Controller B I have this:

SalesAsso user;
public void setUser(SalesAsso user) {
    this.user = user;
}

my controller B object is not null, but it still doesn't set my user as "user". I run the code, it It runs fine. but then when I try to manipulate that salesAsso object, the object is null and It won't let me do anything with it.

Sameersan
  • 1
  • 4
  • Possible duplicate of [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – SedJ601 Mar 27 '18 at 05:15
  • So if did say SalesAssociateScreenController.setSalesAssociate(this.user); would it fix the nullpointer error? – Sameersan Mar 27 '18 at 07:29
  • 1
    please read http://stackoverflow.com/help/mcve and act accordingly – kleopatra Mar 27 '18 at 10:15

1 Answers1

0

I figured out the issue. What I needed to do was store the variable from controller a into a static variable of the same type in controller b. That let me manipulate the object in the controller b class!

Sameersan
  • 1
  • 4
  • hmm ... static sounds ... wrong .. you did read the referenced answer, didn't you :) – kleopatra Mar 29 '18 at 10:24
  • Yea I add. That did help transferring the object from one controller. But then after I transferred it. I referencedd it a static object that was already into my controller to that object. And then that let me manipulate the object with all the different methods and events – Sameersan Mar 30 '18 at 12:55
  • 99% of static fields are .. wrong, so most probably there's something fishy with the "static object that was already" :) – kleopatra Mar 30 '18 at 13:04
  • So do you think I should remove the static field? – Sameersan Mar 30 '18 at 15:24
  • yeah, in the longer run certainly .. depending on your time frame/experience you can mark it as problematic and come back to replace later - don't want to drive you along a road you might not yet ready to go right now :) – kleopatra Mar 30 '18 at 16:11