Leonardo P.
Leonardo P.

Reputation: 41

NullPoinerException in javafx when looking up objects

I have three ellipses that I need to reference in my FXML Controller:

<Ellipse fx:id="selectorontop" id="selectorontop" fill="WHITE" layoutX="121.0" layoutY="101.0" radiusX="14.0" radiusY="27.0" stroke="WHITE" strokeType="INSIDE" style="-fx-opacity: 70%;" visible="false" />
  <Ellipse fx:id="selectoronmiddle" id="selectoronmiddle" fill="WHITE" layoutX="121.0" layoutY="168.0" radiusX="14.0" radiusY="27.0" stroke="WHITE" strokeType="INSIDE" style="-fx-opacity: 70%;" visible="false" />
  <Ellipse fx:id="selectoronbottom" id="selectoronbottom" fill="WHITE" layoutX="120.0" layoutY="466.0" radiusX="14.0" radiusY="27.0" stroke="WHITE" strokeType="INSIDE" style="-fx-opacity: 70%;" visible="false" />

The scene is passed to the controller after it is created:

public class QuickCopy extends Application {

@Override
public void start(Stage stage) throws Exception {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
    AnchorPane root = (AnchorPane)loader.load();

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.sizeToScene();
    stage.setResizable(false);
    stage.show();

    MainController controller = (MainController)loader.getController();
    controller.sendScene(scene);
    System.out.println("new: " +scene.lookup("selectorontop"));
}

The scene is received by the controller, yet the result of the lookup is still "null", both in the main Java file (seen above), and in the Controller, and I can't figure out why.

Thanks in advance

Upvotes: 0

Views: 27

Answers (1)

fabian
fabian

Reputation: 82491

The selector you're using selects by type, not by id. It selects nodes of type selectorontop and I'm pretty sure this type doesn't exist. (At least there are no nodes of this type in the scene.)

You need to use the proper CSS selector. In this case you need to use #selectorontop to select by id:

System.out.println("new: " +scene.lookup("#selectorontop"));

Upvotes: 2

Related Questions