简体   繁体   中英

Why is my BufferedImage receiving a null value from ImageIO.read()

BufferedImage = ImageIO.read(getClass().getResourceAsStream("/Images/player.gif"));

First of all, yes I did add the image folder to my classpath.

For this I receive the error java.lang.IllegalArgumentException: input == null!

I don't understand why the above code doesn't work. From everything I read, I don't see why it wouldn't. I've been told I should be using FileInputStream instead of GetResourceAsStream , but, as I just said, I don't see why. I've read documentation on the methods and various guides and this seems like it would work.

Edit: Okay, trying to clear some things up with regards to what I have in the classpath.

This is a project created in Eclipse. Everything is in the project folder DreamGame , including the "Images" folder. DreamGame is, of course, in the classpath. I know this works because I'm reading a text file in /Images with info on the gif earlier on in the code.

So I have: /DreamGame/Images/player.gif

Edit 2: The line that's currently in the original post is all that's being passed; no /DreamGame/Images/player.gif , just /Images/player.gif . This is from a method in the class ImagesLoader which is called when an object from PlayerSprite is created. The main class is DreamGame . I'm running the code right from Eclipse using the Run option with no special parameters

Trying to figure out how to find which class loader is loading the class. Sorry, compared to most people I'm pretty new at this.

Okay, this is what getClassLoader() gets me: sun.misc.Launcher$AppClassLoader@4ba778

getClass().getResource(getClass().getName() + ".class") returns /home/gixugif/Documents/projects/DreamGame/bin/ImagesLoader.class

The image file is being put in bin as well. To double check I deleted the file from bin , cleaned the project, and ran it. Still having the same problem, and the image file is back in bin

Basically, Class.getResourceAsStream doesn't do what you think it does.

It tries to get a resource relative to that class's classloader - so unless you have a classloader with your filesystem root directory as its root, that won't find the file you're after.

It sounds like you should quite possibly really have something like:

BufferedImage = ImageIO.read(getClass().getResourceAsStream("/Images/player.gif"))

(EDIT: The original code shown was different, and had a full file system path.)

and you make sure that the images are copied into an appropriate place for the classloader of the current class to pick up the Images directory. When you package it into a jar file, you'd want the Images directory in there too.

EDIT: This bit may be the problem:

First of all, yes I did add the image folder to my classpath.

The images folder shouldn't be in the classpath - the parent of the Images folder should be, so that then when the classloader looks for an Images directory, it will find it under its root.

If you use resourceAsStream "/" referes to the root of the classpath entry, not to the root of the file system. looking at the path you are using this might be the reason.

If you load something from some home path you probably should use a FileInputStream. getResourceAsStream is for stuff that you deploy with your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM