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.