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

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

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):

  1. public static Render plane = loadBitmap("/textures/floor.png");
  2. public static Render loadBitmap(String fileName) {
  3. try {
  4. BufferedImage image = ImageIO.read(Texture.class.getResourceAsStream(fileName));
  5. int width = image.getWidth();
  6. int height = image.getHeight();
  7. Render result = new Render(width, height);
  8. image.getRGB(0, 0, width, height, result.pixels, 0, width);
  9. return result;
  10. } catch (Exception e) {
  11. Debug.logError("CRASH | Failed to load image");
  12. throw new RuntimeException(e);
  13. }
  14. }

However, I'm encountering the following errors:

  1. Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: input == null!
  2. at net.xernia.bluehero.graphics.Texture.loadBitmap(Texture.java:23)
  3. at net.xernia.bluehero.graphics.Texture.<clinit>(Texture.java:11)
  4. ... 5 more
  5. Caused by: java.lang.IllegalArgumentException: input == null!
  6. at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1356)
  7. at net.xernia.bluehero.graphics.Texture.loadBitmap(Texture.java:15)
  8. ... 6 more

My project structure is as follows:

  1. src
  2. package
  3. Texture.java
  4. res
  5. textures
  6. 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)

  1. public static Render plane = loadBitmap(&quot;/textures/floor.png&quot;);
  2. public static Render loadBitmap(String fileName) {
  3. try {
  4. BufferedImage image = ImageIO.read(Texture.class.getResourceAsStream(fileName));
  5. int width = image.getWidth();
  6. int height = image.getHeight();
  7. Render result = new Render(width, height);
  8. image.getRGB(0, 0, width, height, result.pixels, 0, width);
  9. return result;
  10. }catch (Exception e) {
  11. Debug.logError(&quot;CRASH | Failed to load image&quot;);
  12. throw new RuntimeException(e);
  13. }
  14. }

and I am receiving the following errors:

  1. Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: input == null!
  2. at net.xernia.bluehero.graphics.Texture.loadBitmap(Texture.java:23)
  3. at net.xernia.bluehero.graphics.Texture.&lt;clinit&gt;(Texture.java:11)
  4. ... 5 more
  5. Caused by: java.lang.IllegalArgumentException: input == null!
  6. at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1356)
  7. at net.xernia.bluehero.graphics.Texture.loadBitmap(Texture.java:15)
  8. ... 6 more

my project structure is as follows:

  1. src
  2. package
  3. Texture.java
  4. res
  5. textures
  6. 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,我们有:

  1. src
  2. package
  3. Texture.java
  4. res
  5. textures
  6. floor.png

并将文件名更改为 /res/textures/floor.png。(不要忘记重新构建项目)

英文:

Move folder res to src, we have:

  1. src
  2. package
  3. Texture.java
  4. res
  5. textures
  6. 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:

确定