1

I am using IntelliJ to build a .jar of my project, but whenever i call any of the following:

getClass().getClassLoader().getResourceAsStream();
getClass().getClassLoader().getResource();
getClass().getResource();
getClass().getResourceAsStream();

I ALWAYS get this error:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1358)
    at Objects.Collsion.TexturedBarrier.setTexture(TexturedBarrier.java:34)
    at Objects.Collsion.TexturedBarrier.<init>(TexturedBarrier.java:20)
    at Levels.Menu.MainMenu.load(MainMenu.java:21)
    at Levels.Level.loadLevel(Level.java:36)
    at Main.GamePanel.nextLevel(GamePanel.java:127)
    at Main.GamePanel.setStage(GamePanel.java:108)
    at Main.GamePanel.<init>(GamePanel.java:28)
    at Main.Game.main(Game.java:23)

The class causing the error is:

public class TexturedBarrier {

    protected BufferedImage currentTexture;

    public TexturedBarrier(String texture) {
    
        setTexture(texture);
    }

    public void setTexture(String texture) {

        InputStream stream = this.getClass().getClassLoader().getResourceAsStream(texture);

        try {
            currentTexture = ImageIO.read(stream);
        }catch (IOException e) {e.printStackTrace();}
    }

}

And the code calling it is:

new TexturedBarrier("Play!_Background_green.jpg");

The resource folder is set as a resource root in IntelliJ, and the resource folder is as follows: res folder

and after using 7zip to inspect the jar, the file is definately in the jar: inside jar

Any ideas? i know this is an issue with alot of similar questions, but none of the solution posed have helped me.

1 Answers1

0

Check the documentation of getResourceAsStream(): Returns: A InputStream object; null if no resource with this name is found, the resource is in a package that is not open to at least the caller module, or access to the resource is denied by the security manager.

So now you have three reasons why the inputstream you are trying to read from is null. The most likely reason is that the resource is not found where you are expecting it (the name you specified). This has been discussed in length in getResourceAsStream returns null

Queeg
  • 7,748
  • 1
  • 16
  • 42