英文:
How can I fix an error with loading textures using ImageIO.read [Java] [Beginner]
问题
I recently decided to try getting back into Java after being away from it for many years. I'm following a YouTube tutorial series to create a game. I attempted to load a texture using the code provided below (I'm using Eclipse):
public static Render plane = loadBitmap("/textures/floor.png");
public static Render loadBitmap(String fileName) {
    try {
        BufferedImage image = ImageIO.read(Texture.class.getResourceAsStream(fileName));
        int width = image.getWidth();
        int height = image.getHeight();
        Render result = new Render(width, height);
        image.getRGB(0,  0, width, height, result.pixels, 0, width);
        return result;
    } catch (Exception e) {
        Debug.logError("CRASH | Failed to load image");
        throw new RuntimeException(e);
    }
}
However, I'm encountering the following errors:
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: input == null!
	at net.xernia.bluehero.graphics.Texture.loadBitmap(Texture.java:23)
	at net.xernia.bluehero.graphics.Texture.<clinit>(Texture.java:11)
	... 5 more
Caused by: java.lang.IllegalArgumentException: input == null!
	at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1356)
	at net.xernia.bluehero.graphics.Texture.loadBitmap(Texture.java:15)
	... 6 more
My project structure is as follows:
src
   package
       Texture.java
res
   textures
       floor.png
Under Properties -> Java Build Path, I've added "res" as a class folder under the module path. I apologize if this is a basic error, but it's quite confusing for me. I hope this information is sufficient, and please let me know if I missed any crucial details ![]()
英文:
I recently decided to try and get back into java after many years away from it and am following a youtube tutorial series for making a game. I have tried to load a texture I made using the following code
(I am using Eclipse)
public static Render plane = loadBitmap("/textures/floor.png");
public static Render loadBitmap(String fileName) {
	try {
		BufferedImage image = ImageIO.read(Texture.class.getResourceAsStream(fileName));
		int width = image.getWidth();
		int height = image.getHeight();
		Render result = new Render(width, height);
		image.getRGB(0,  0, width, height, result.pixels, 0, width);
		return result;
	}catch (Exception e) {
		Debug.logError("CRASH | Failed to load image");
		throw new RuntimeException(e);
	}
}
and I am receiving the following errors:
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: input == null!
at net.xernia.bluehero.graphics.Texture.loadBitmap(Texture.java:23)
at net.xernia.bluehero.graphics.Texture.<clinit>(Texture.java:11)
... 5 more
Caused by: java.lang.IllegalArgumentException: input == null!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1356)
at net.xernia.bluehero.graphics.Texture.loadBitmap(Texture.java:15)
... 6 more
my project structure is as follows:
 src
     package
         Texture.java
 res
     textures
         floor.png
I have under Properties->java build path added res as a class folder under modulepath. Sorry if this is a very trivial error to some of you, but it is confusing me to no end lol. Hope this is enough information, please let me know if there was something vital I left out ![]()
答案1
得分: 2
将文件夹 res 移动到 src,我们有:
src
    package
         Texture.java
    res
         textures
             floor.png
并将文件名更改为 /res/textures/floor.png。(不要忘记重新构建项目)
英文:
Move folder res to src, we have:
src
    package
         Texture.java
    res
         textures
             floor.png
and change file name to /res/textures/floor.png. (Don't forget rebuild project)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论