1

I am very new to javafx, and was getting java.lang.reflect.InvocationTargetException when testing with the code tutorial:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

    public static final String IMAGE_NAME = "groceries.jpg";

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(setupScene(), 300, 300);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Image Screen");
        primaryStage.show();
    }

    StackPane setupScene() {
        StackPane root = new StackPane();

        ImageView imageView = new ImageView();
        imageView.setPreserveRatio(true);
        imageView.setSmooth(true);

        Image image = new Image(getClass().getClassLoader().getResource(IMAGE_NAME).toString());
        imageView.setImage(image);

        root.setPrefSize(250, 250);
        imageView.setFitHeight(root.getPrefHeight());
        imageView.setFitWidth(root.getPrefWidth());
        root.getChildren().add(imageView);

        return root;
    }

}

The exception was caused by java.lang.NullPointerException in line Image image = new Image(getClass().getClassLoader().getResource(IMAGE_NAME).toString());

The image file is in my project folder, but it doesn't seem to be loaded. I was able to get the image using Image image = new Image(new File(IMAGE_NAME).toURI().toURL().toString()), but when I switched back to Image image = new Image(getClass().getClassLoader().getResource(IMAGE_NAME).toString()), it just never worked.

Does anyone know why my program is behaving like this? Any ideas would be highly appreciated...

Edit: My image file is on the same level of the src folder:

- projectfolder
     - groceries.jpg
     - src
        - Main.java

I'm using IntelliJ JavaFX Application to create the project, everything is in default state.

2 Answers2

0

Image image = new Image(getClass().getClassLoader().getResource(IMAGE_NAME).toString()) and Image image = new Image(new File(IMAGE_NAME).toURI().toURL().toString()) do two different things.

The first will get a resource based off the class loader, which is most commonly used when extracting a resource directly from the jar file. The second is used to load the image from the file system, which is why that works in your case (that's where the file is!)

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
0

The getClass().getClassLoader().getResource() line, searchs for the file in the same location where your Main class is.

If you have this image in the src, you just need to add a '/' :

private String theme1Url = getClass().getResource("/groceries.jpg").toExternalForm();
gonzaloan
  • 371
  • 2
  • 13
  • Thanks! It worked after I moved the image file inside the src folder. But adding a slash still gave me this exception :-[ – Momo Hanakawa Nov 28 '18 at 20:59
  • If you move the file inside src you don't need the slash, the slash is just if you have the file in the project folder :) – gonzaloan Nov 29 '18 at 12:18