I want to read Images (.png files) in my project, and I want this to work in a runnable .jar file too. So I wrote this little piece of code:
try {
InputStream in;
in = Loader.class.getClassLoader().getResourceAsStream("buttons.png");
System.out.println(in.read() + ", Reader: " + in);
BufferedImage img = ImageIO.read(in);
System.out.println(img.getHeight());
in.close();
} catch (IOException e) {
e.printStackTrace();
}
When I run it, I get the following output:
137, Reader: java.io.BufferedInputStream@15db9742
Exception in thread "main" java.lang.NullPointerException
at test.Loader.load(Loader.java:21)
at test.MainTest.main(MainTest.java:6)
My MainTest does nothing but running this code, so I won't include it here.
I already tested if the InputStream is null as you may have noticed. As it obviously isn't the path to my file has to be right. My question is: Why is ImageIO.read() returning null?
buttons.pngis at the root directory of the class path of Loader. Case-sensitive, Inspect the .jar file with 7zip, WinZip or the like.