0

I am slowly teaching myself how to code and have come across something that has stumped me

I am getting a nullpointer exception when using setImage to change an image around. I am aware this is possibly down to the imageview not being initialized when I am making the change, but I am not sure if that is the case.

I currently have this piece of code which is dealing with my scene switches

public void initSceneSwitch(String scr) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/main/resources/fxml/" + scr + ".fxml"));

            sceneDisplayed = new Scene(root);
            sceneDisplayed.getStylesheets().add(getClass().getResource("/main/resources/css/GameTheme.css").toExternalForm());          
            primaryStage.setScene(sceneDisplayed);
            primaryStage.show();
            sceneChange(scr);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

This will deal with image switches, in the long term, with the possibility of more variables which determine which image to display

public void sceneChange(String newScene) {
        switch (newScene) {
            case "UILoadingScreen":

                break;
            case "UIMainScreen":
                UIMainScreen uiMS = new UIMainScreen();

                Image img= new Image("/main/resources/icons/test.png");
                uiMS.getImageView().setImage(img);
                uiMS.getImageViewAnchorPane().getChildren().add(uiMS.getImageView());
                break;              
            default:

                break;
        }

    }

The nullpointer exception occurs on uiMS.getImageView().setImage(img);. This is where I am confused, from thinking about how my code is running "along a line", I would think that both my controller and fxml file would have been loaded and initialized before I am making the method call to change images around.

Can anyone point me in the general direction of what I need to be looking at, because my brain hurts

Fluke
  • 63
  • 5
  • How do you initialize the image view in your uiMS class? – Tobias G Feb 02 '17 at 14:14
  • I always do a "if(file.exits){System.out.println("file exits");}" to make sure everything is working fine. – SedJ601 Feb 02 '17 at 14:24
  • Use Fernando Paz answer. http://stackoverflow.com/questions/22710053/how-can-i-show-an-image-using-the-imageview-component-in-javafx-and-fxml – SedJ601 Feb 02 '17 at 15:48

0 Answers0