0

I have the following project setup (Maven Project):

project setup

And the absolute path: C:\Users\jenny\IdeaProjects\gameapp\client\src\main\resources\images\game\black_market.png

This code throws a Nullpointerexception:

    private void setUpImage(ImageView image){
    Platform.runLater(()->{
        var url = getClass().getResourceAsStream("images/game/black_market.png");
        System.out.println(url.toString());
        Image img = new Image(url);
        image.setImage(img);
    });
}

It's probably because the filepath is incorrect but I don't really know how to fix it on my own. I've tried for the last 2-3 hours researching similar problems on Stackoverflow.

The ImageView that gets passed on this function is a reference to an ImageView. My function should load the image and put it into the passed ImageView. To be honest, I don't actually know if that will work since Java is pass by value.

Jenny
  • 1
  • 3
    Try using `var url = getClass().getResourceAsStream("/images/game/black_market.png");` instead. A relative path will look within the current package for the resource, where as using `/` will look at the top of the class path. Also, unzip the resulting JAR file and check the images are been included within the JAR file – MadProgrammer Dec 30 '21 at 08:40
  • Hey, thanks for the quick reply. I've tried it but still get: Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found. The Images are included in the compiled classes/JAR. – Jenny Dec 30 '21 at 08:46

1 Answers1

0

why setImage() doesn't change my ImgView in JavaFx?

This question fixed it. I've noticed that the @FXML annotations were missing. Adding them fixed the entire problem.

@FXML
private void setUpImage(ImageView image){
    Platform.runLater(()->{
        Image img = new Image("/images/game/black_market.png");
        image.setImage(img);
    });
}
Jenny
  • 1