Jolly Roger
Jolly Roger

Reputation: 1134

Getting reference to FXML ImageView

I am making a game in JavaFX , for which I have 5x9 grid of ImageViews and also some Labels. When I try to get reference to label , I am successful. However , when I try to get reference to ImageView , I get nullPointerException.

Here is how root is declared :

FXMLLoader loaderTwo = new FXMLLoader(getClass().getResource("/game/levelTwo.fxml"));
Parent rootTwo = loaderTwo.load();
this.handlerTwo = loaderTwo.getController();

Here is FXML part of label :

<Label fx:id="sunCountLabel" layoutX="121.0" layoutY="15.0" minHeight="72.2891845703125" prefHeight="88.0" prefWidth="203.0" text="Label">
  <font>                                                                                                                                  
    <Font name="Gabriola" size="60.0" />                                                                                                  
  </font>                                                                                                                                 
</Label> 

FXML part of Image view :

<ImageView fx:id="OneFive" fitHeight="180.0" fitWidth="180.0" layoutX="892.0" layoutY="102.0">
  <effect>                                                                                   
    <DropShadow height="1.0" radius="0.0" width="1.0" />                                     
  </effect>                                                                                  
  <image>                                                                                    
    <Image url="@../media/half.png" />                                                       
  </image>                                                                                   
</ImageView>        

This is where I refer to them :

this.root = this.map.getLevel(2).getRoot();
Label l = (Label)this.root.lookup("#sunCountLabel");
ImageView i = (ImageView)this.root.lookup("OneFive");
System.out.println(i==null);
l.setText("1999");

Issue is that Label is working correctly but Imageview gives nullPointerException
I can't see why ?
Also , the scene is loading perfectly , no issues with image address etc.

Upvotes: 1

Views: 270

Answers (1)

fabian
fabian

Reputation: 82461

The selector you're using is a type selector, i.e. it would only work, if a node returned "OneFive" from the getTypeSelector method. ImageView doesn't do this.

You probably wanted to use the id selector. This requires you to insert a # at the start of the selector string:

ImageView i = (ImageView)this.root.lookup("#OneFive");

I strongly recommend injecting the ImageView to a field in the controller though and accessing the ImageView via the controller. Also note that there are cases when lookup doesn't work. Looking up content inside a ScrollPane e.g. does not work before the first layout pass, since the node structure the content is added to is created during the first layout pass.

Upvotes: 2

Related Questions