Garmaine Staff asked 1 year ago

Iam coding game as part of my homework and I'm trying to load a image from res folder but I'm getting NullPointerException: Exception in thread "Thread-0" java.lang.IllegalArgumentException: input == null! at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1400) at prg_hra/esrever.BufferedImageLoader.loadImage(BufferedImageLoader.java:15) at prg_hra/esrever.Hra.init(Hra.java:38) at prg_hra/esrever.Hra.run(Hra.java:61) at java.base/java.lang.Thread.run(Thread.java:834)

The res folder is already in Java Build Path and I'm 99% sure that I'm putting in the right path. I got BufferedImageLoader Class:

public class BufferedImageLoader {

private BufferedImage image;

public BufferedImage loadImage(String path) {

    try {
        image = ImageIO.read(getClass().getResource(path));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return image;
}

and I'm using it in inti metode in game class: private void init() {

    WIDTH=getWidth();
    HEIGHT=getHeight();

    BufferedImageLoader loader = new BufferedImageLoader();
    level = loader.loadImage("/res/level.png");     

    handler = new Handler();

    kam=new Kamera(0,0);

    handler.addObjekt(new Hrac(100, 100, handler, ObjektID.Hrac));

    handler.createLevel();

    this.addKeyListener(new KeyInput(handler));
}

I've looked at some similar problems of other people, but they mostly just used wrong path..

EDIT: I also tried level = loader.loadImage("level.png"); and level = loader.loadImage("/level.png"); neither worked.