0

I know what the problem is I just do not know how to fix it. So I have an image that I am trying to render in my program. I use ImageIO to load the image. But it seems to have a problem wit the path I am giving it. I am using NetBeans as my IDE and I dont know if I am saving the image file correctly.

First method:

public void init(){
    BufferedImageLoader loader = new BufferedImageLoader();
    try{
        spriteSheet = loader.loadImage("/sprite_sheet.png");

    }catch(IOException e){
        e.printStackTrace();
    }

    SpriteSheet ss = new SpriteSheet(spriteSheet);
    player = ss.grabImage(1,1,32,32);
}

the loader BufferedImageLoader class:

public class BufferedImageLoader {

    private BufferedImage image;

    public BufferedImage loadImage(String path) throws IOException{
        image = ImageIO.read(getClass().getResource(path));
        return image;
    }

}

I have the image saved under a 'res' folder under 'src' folder.

Error: Exception in thread "Thread-2" java.lang.IllegalArgumentException: input == null!

Thank you.

2 Answers2

0

Try using an absolute path for your file or if you need a relative check this post (eg assuming you have a res folder under default package did you try "/res/yourfile"

Community
  • 1
  • 1
Kostas Kryptos
  • 4,081
  • 2
  • 23
  • 24
0

Why do you need to use getClass().getResource() ?

Most simple usage of ImageIO.read is as follows.

image = ImageIO.read(new File(path));

You may need to add folders to path also.

spriteSheet = loader.loadImage("/src/res/sprite_sheet.png");
Fumu 7
  • 1,091
  • 1
  • 7
  • 8