首页 > 解决方案 > 修复输入 == null!创建图像时出错

问题描述

我正在尝试加载精灵表,但它不起作用。它在线程“Thread-0”java.lang.IllegalArgumentException 中给出错误异常:输入 == null!

我将图像保存到文件夹 } res --> Textures --> sheet.png 。我通过构建路径确保 res 文件夹位于 Class 文件夹中。

请帮忙

        //  public class Game 
   private void init(){
    display = new Display(title, width, height);
    test = BufferedImageLoader.loadImage("/sheets.png");
    Assets.init();
} 

  private void render(){
    bs = display.getCanvas().getBufferStrategy();
    if(bs == null){
        display.getCanvas().createBufferStrategy(3);
        return;
    }
    g = bs.getDrawGraphics();

    g.clearRect(0, 0, width, height);


    g.drawImage(test, x, 10, null);

    //End Drawing!
    bs.show();
    g.dispose();
}  

// 公共类 BufferedImageLoader {

public static BufferedImage loadImage(String path){
    try {
        return ImageIO.read(BufferedImageLoader.class.getClassLoader().getResourceAsStream(path));
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
    return null;
}

}

标签: javainput

解决方案


弄清楚了!使用源文件夹中的所有图像创建一个单独的文件夹。然后,使用链接 /folder name/ photo name 。格式(例如“/Images/mario.png”)。这种方式有效。


推荐阅读