0

I have my JavaFX app.

I'm trying to display some images on Scene it looks like this:

    private ImageView createChampionImageView(Champion champion)
    {
        ImageView imageView = new ImageView("/images/champions/" + champion.getName().toLowerCase().replaceAll("[. ]", "") + ".png");
        imageView.setFitHeight(60);
        imageView.setFitWidth(60);
        RoundCorners.setRoundedCornerImageView(imageView);
        return imageView;
    }

My error looks like this and it only happens to JAR file, if i run the program in IntelliJ everything is fine:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "java.net.URL.toString()" because the return value of "java.lang.Class.getResource(String)" is null
    at me.trup10ka.jlb.controllers.ChampionsScene.createChampionImageView(ChampionsScene.java:138)
    at me.trup10ka.jlb.controllers.ChampionsScene.addToTilePane(ChampionsScene.java:91)
    at me.trup10ka.jlb.controllers.ChampionsScene.fillChampionsPane(ChampionsScene.java:75)
    at me.trup10ka.jlb.controllers.MainScene.lambda$setChampionsParserUGG$3(MainScene.java:48)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
    at java.base/java.lang.Thread.run(Thread.java:833)

I'm calling this method (createChampionImageView()) in this method:

public void fillChampionsPane()
    {
        if (championsPageParser.champions() == null)
            return;
        ArrayList<Champion> champions = championsPageParser.champions();
        for (Champion champion : champions)
        {
// This addToTilePane calls the createChampionImageView
            addToTilePane(champion);
        }
    }

And this method above is called from here (it is called from a different controller):

@FXML
    private void setChampionsParserUGG()
    {
        JavaLeagueBuilds.chosenPage = U_GG;
        JavaLeagueBuilds.getInstance().switchToLoading();
        CompletableFuture.runAsync(() -> ChampionsScene.getInstance().setChampionsPageParser(new HtmlChampionsUGGParser()))
                .thenRun(() -> Platform.runLater(() -> {
                    ChampionsScene.getInstance().fillChampionsPane();
                    JavaLeagueBuilds.getInstance().switchToChampions();
                }));
    }

I tried using getClass().getResource() and getResourceAsStream() but it seems nothing has worked.

EDIT: So I tried the getResource() again following the guide, but when I run my program as JAR i get this:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because the return value of "java.lang.Class.getResource(String)" is null
    at me.trup10ka.jlb.controllers.ChampionsScene.createChampionImageView(ChampionsScene.java:141)
    at me.trup10ka.jlb.controllers.ChampionsScene.addToTilePane(ChampionsScene.java:94)
    at me.trup10ka.jlb.controllers.ChampionsScene.fillChampionsPane(ChampionsScene.java:78)
    at me.trup10ka.jlb.controllers.MainScene.lambda$setChampionsParserUGG$3(MainScene.java:48)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
    at java.base/java.lang.Thread.run(Thread.java:833)

Process finished with exit code 0

What is very strange that the app icon is loaded normaly and works but other images dont.

Here is my github repository if you need to check anything else

Trupka
  • 1
  • 2
  • 1
    there is no way around _learning_ how to lookup resources (vs. random experimenting :) see f.i. the duplicate which explains in detail what/how to do it. – kleopatra Apr 25 '23 at 15:41
  • 1
    That code cannot possibly produce that exception, unless I am missing something; – James_D Apr 25 '23 at 16:00
  • 1
    Did you actually check to see if the image is in the jar file? – James_D Apr 25 '23 at 16:48

0 Answers0