1

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)
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
foobarbot
  • 13
  • 2

2 Answers2

2

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

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.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • Ok, thanks I understand. How can I code it so it will work from other packages? Is passing a URL from the a package near the images folder the only way for it to work? – foobarbot Aug 12 '11 at 21:27
  • Just give getResource() an absolute path like "/com/blah/bleh/images/meganium.png". Note the leading '/'. – Ryan Stewart Aug 12 '11 at 21:33
  • Weird, it still returns null. I dug up some older test apps that I had written and they seem to work fine. In fact in one of them it was able to load using "images/image.png" from a package next to the images package(foo/bar/images and foo/bar/classes in case I was confusing). I am completely confused. I did some more testing including putting the image in the same folder as the class and it still returned null. – foobarbot Aug 13 '11 at 01:37
  • I was fumbling through netbeans and noticed it wasnt running jdk7 so I changed the conf file and now my images load correctly! However my text in netbeans got messed up but one battle at a time I say. – foobarbot Aug 13 '11 at 02:08