I know that this question had been answered too many times but I couldn't fix it no matter solution I tried.
I want to use a jpeg image as background but I can't resolve it no matter I tried.
Below is my final package structure :
images/
-- bg.jpeg
org/
-- Main.java (used for test)
Code
public class Main {
BufferedImage img;
public static void main(String[] args) {
Main main = new Main();
main.load();
}
public void load(){
try {
ClassLoader cl = this.getClass().getClassLoader();
System.out.println("CL:"+cl);
InputStream url = getClass().getClassLoader().getResourceAsStream("/images/bg.jpg");
System.out.println("URL:"+url);
this.img = ImageIO.read(url); // Null argument exception
} catch (IOException ex) {
Logger.getLogger(BoardView.class.getName()).log(Level.SEVERE, null, ex);
}
}}
Output
CL:sun.misc.Launcher$AppClassLoader@15663a2
URL:null
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1348)
at org.Main.load(Main.java:32)
at org.Main.main(Main.java:24)
I am using JDK7 and Maven project.