0

I'm trying to use ImageIO.read to load an image as a BufferedImage.

Here's the code:

public class Sprite {
    private BufferedImage image;

    public Sprite(String img) {
        URL imageUrl = Sprite.class.getResource(img);
        try {
            BufferedImage image = ImageIO.read(imageUrl);
        } catch (IOException e) {}
    }
}

public class Main {
    public static void main(String[] args) {
        Sprite sprite = new Sprite("../images/house.png");
    }
}

The directory my code is in looks like this:

project
  | -- src
  |       | -- Main.java
  | -- images
  |       | -- house.png

And the exact error I get:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1388)
    at game.Sprite.<init>(Sprite.java:12)
    at game.Main.main(Main.java:5)

Changing the input URL from "../images/house.png" to "/../images/house.png" does not fix nor change this error.

The image is in the same folder as the .class files.

If it helps, I'm using IntelliJ IDEA.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jaxben
  • 21
  • 6
  • 2
    Does this answer your question? [Getting a BufferedImage as a resource so it will work in JAR file](https://stackoverflow.com/questions/17007448/getting-a-bufferedimage-as-a-resource-so-it-will-work-in-jar-file) – Aposhian Oct 30 '20 at 22:38
  • Thank you Aposhian, your solution worked. – jaxben Oct 30 '20 at 22:44

0 Answers0