如何修复使用ImageIO.read加载纹理时出现的错误 [Java] [初学者]

huangapple go评论133阅读模式
英文:

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 如何修复使用ImageIO.read加载纹理时出现的错误 [Java] [初学者]

英文:

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(&quot;/textures/floor.png&quot;);

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(&quot;CRASH | Failed to load image&quot;);
		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.&lt;clinit&gt;(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 如何修复使用ImageIO.read加载纹理时出现的错误 [Java] [初学者]

答案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)

huangapple
  • 本文由 发表于 2020年8月12日 09:22:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63368428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定