1

I have some sort of problem during migration from javafx 2.2 to javafx 8; Javafx 8 doesn't support static @FXML elements.

I have the following situation (code is simplified):

Parent controller:

public abstract class ParentController implements Initializable {

@FXML
protected Label parentLabel;

@Override
public void initialize(URL location, ResourceBundle resources) {
    // do smth
}

}

Child Controller (there is not only one child):

public class ChildController extends ParentController {

@Override
public void initialize(URL location, ResourceBundle resources) {
    super.initialize(location, resources); 

    parentLabel.setText("text");
}

}

In previous version of this code parentLabel was static and it worked fine. But now I have NullPointerException while trying to setText on label.

I'm looking for the simplest solution in this situation.

UPD 1.

child fxml:

<AnchorPane id="" fx:id="apDentalClinics" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="590.0" prefWidth="1200.0" style="" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="com.package.ChildController">
<children>
    //Something here, except our lable
</children>
</AnchorPane>
Vasyl Hoshovsky
  • 237
  • 1
  • 5
  • 13
  • I just pasted your code verbatim and it worked just fine. Can you give an example of an FXML file and an `Application` subclass that make this fail? – James_D Aug 19 '15 at 14:44
  • I don't understand the FXML you posted. If you don't have a `` then necessarily `parentLabel` will be `null` and you get a `NullPointerException`. This has nothing to do with inherited controllers, it's just because you don't have a node with a matching `fx:id`. If you have `` then it works as expected. Can you explain more fully what you are actually trying to do? – James_D Aug 19 '15 at 18:06
  • Well, you're right. I should have provide more info. Label "parentLabel" exsist in another child fxml and have to be initialized when this fxml is invoked by ApplicationMainClass. After that child window throws exception. When we init child controller, is parent controller initializes as well? – Vasyl Hoshovsky Aug 19 '15 at 18:34
  • 1
    Yes... the parent controller's initialize() method will be called, because you explicitly do that. But this is simply the wrong approach to whatever it is you are trying to do. `parentLabel` is an instance variable, so the first child controller will initialize the `parentLabel` field belonging to that controller instance. It obviously won't initialize the `parentLabel` belonging to other controller instances. On the other hand, you certainly do not want `parentLabel` to be `static`, as all controller instances would share the same label, and you can't add the same node twice in the scene. – James_D Aug 19 '15 at 18:50
  • 1
    So it seems like you are basically trying to use this as a hack to share some data (the text of the label) between two different controllers? Is that right? If so, your question is really "how do I share data between controllers?" (and this is completely the wrong approach). So you should probably try to think about the problem from that perspective and then post a new question along those lines if needed. – James_D Aug 19 '15 at 18:52
  • Previous developer made this label static to share it. But now i need to migrate this app from javafx 2 to javafx 8. Static fields are not allowed. So I can't implement authors idea. – Vasyl Hoshovsky Aug 19 '15 at 19:17
  • Do you have any suggestions how to handle this situation and make it work? – Vasyl Hoshovsky Aug 19 '15 at 19:17
  • 1
    Have a look at http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/14190310#14190310 and see if it gets you started. Frankly, static `@FXML`-injected fields are nonsense, so you may have a bit of rewriting to do. Think in terms of sharing the data: so you can think in terms of making a `StringProperty` available to the different controllers. – James_D Aug 19 '15 at 19:59

0 Answers0