开发者

Java bufferedimage not loading

开发者 https://www.devze.com 2023-03-27 18:11 出处:网络
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:

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)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号