How can I fix an error with loading textures using ImageIO.read [Java] [Beginner]

Viewed 78

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 Answers

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)

Related