foobarbot
foobarbot

Reputation: 13

Java bufferedimage not loading

I have this image that I want to load but it always gives me an input= null exception. This is the first bit of code:

Entity e = new Entity("images/meganium.png");

Here is the part that loads the image:

image = null;
    try{
        path = this.getClass().getResource(fileName);
        System.out.println(path);
        image = ImageIO.read(path);
    }catch(IOException e){
        System.out.println("Dun goofed in " + "SPrites");
    }

The structure is like this:

com/blah/bleh/Main
com/blah/bleh/images
com/blah/bleh/foo/bar/Loader Class

Stack trace:

java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1362)
at net.ofn.nyc.javagentleman.src.graphic.Sprite.<init>(Sprite.java:31)
at net.ofn.nyc.javagentleman.src.ent.Entity.<init>(Entity.java:21)
at net.ofn.nyc.javagentleman.JavaGentleman.<init>(JavaGentleman.java:27null)
at net.ofn.nyc.javagentleman.JavaGentleman.main(JavaGentleman.java:23)

Upvotes: 1

Views: 435

Answers (2)

Ryan Stewart
Ryan Stewart

Reputation: 128779

You're using a relative path for the image resource. With Class.getResource(), relative paths are resolved against the package containing the class, so if the class loading the images is in package com.blah.bleh.foo.bar, then it will be looking for the image at /com/blah/bleh/foo/bar/images/meganium.png. getResource() returns null if it can't find the given resource and hence your IllegalArgumentException.

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168825

Do you have 2 separate directories, one named images the other named Images (capital 'I')? Keep it consistent. Your file system may not distinguish between the two, but Java does.

Upvotes: 2

Related Questions