0

I've checked my imports and I've checked that that the fx:id of my ImageView and the variable name I gave it in the controller class match, but it still won't work. In other subroutines in this class, other things like buttons and labels are working fine so I don't know what's wrong with this ImageView.

This is my FXML Document:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/10.0.1" fx:controller="application.Controller">
   <children>
      <ImageView fx:id="body" fitHeight="150.0" fitWidth="200.0" layoutX="243.0" layoutY="100.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../img/BB8Body.png" />
         </image>
      </ImageView>
   </children>
</AnchorPane>

And here is the part of the controller that won't work:

package application;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Controller {

@FXML
public ImageView  body = new ImageView();

public void level1(ActionEvent event) {
    //changing scenes to level 1
    Scene scene=null;
    try {
        Parent root = FXMLLoader.load(getClass().getResource("Level1.fxml"));
        scene = new Scene(root);
        Stage primaryStage=(Stage)((((Node) event.getSource()).getScene().getWindow()));
        primaryStage.setScene(scene);
        primaryStage.show();
        initData();
        //lev1=true;
    } catch(Exception e) {
        e.printStackTrace();
    }

    scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent event) {
            switch (event.getCode()) {
            case UP:    
                body.setLayoutY(body.getLayoutY() - 5); 
                System.out.println("UP");
                break;
            case RIGHT: 
                body.setLayoutX(body.getLayoutX() + 5); 
                System.out.println("RIGHT");
                break;
            case DOWN:  
                body.setLayoutY(body.getLayoutY() + 5); 
                System.out.println("DOWN");
                break;
            case LEFT:  
                body.setLayoutX(body.getLayoutX() - 5); 
                System.out.println("LEFT");
                break;
            default:
                break;
            }     
        }
    });
}
  • You are reinitializing the ImageView after it has been imported. Remove the initialization. – Hypnic Jerk Dec 26 '18 at 18:38
  • There don't seem to be any other nodes injected to the controller. I've got no idea how the `level1` is invoked, but I assume you invoke it on a different instance than the one used with the fxml you show on screen. Please provide a [mcve]. – fabian Dec 26 '18 at 18:45
  • @HypnicJerk if I remove the initialisation it gives me a null pointer exception – user10836252 Dec 27 '18 at 10:20
  • @fabian i deleted some of the controller class as I didn't think it was of any use here but i could post the whole thing if you think it would be more helpful? – user10836252 Dec 27 '18 at 10:21
  • I'm more interested in how you invoke the `level1` method (and how you get access to the `Controller` instance it's called for, if it's not called from a event hander method inside `Controller`, which doesn't seem to be the case, since the there are no event handlers registered in the fxml). Also please describe what exactly "not working" means: What did you expect and how is the actual result different? – fabian Dec 27 '18 at 11:08
  • @fabian Level1 is called when you press a button on my menu screen which is a different FXML document. It works as the level 1 screen shows, and when I press the arrow keys the `system.out.println()` works so it's definitely a problem with the picture. – user10836252 Dec 27 '18 at 11:13
  • `FXMLLoader` creates new instances of the controller class every time you call `load` (assuming the loaded fxml contains the `fx:controller` attribute).If you access the `level1` method from a controller created when loading a fxml not containing `body`,you modify a node that is not part of any scene.Note that initializing a field that is supposed to be injected by `FXMLLoader` in 99% of the cases only fixes the symptoms (the exception),not the real issue (working with the wrong controller instance).May be interesting: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian Dec 27 '18 at 11:27
  • @fabian thank you for your help – user10836252 Dec 27 '18 at 11:38

0 Answers0