1

I have scoured this site, and been looking over solutions on how to fix this, and none have given a solution that changes the error occurring. I cannot pinpoint what the problem is. None of the similar questions regarding this have helped.

I'll start with the code I have

public static BufferedImage getImage(String uri) throws IOException {
    BufferedImage image = null; 
    image = ImageIO.read(ImageLoader.class.getResource(uri));

    return image;
}

And where I call the method

try {
    sprite = ImageLoader.getImage("images/testSprite.png");
} catch (IOException e) {
    e.printStackTrace();
}

The error returned is IllegalArgumentException input == null on the ImageIo.read line in the original method.

To preface, I have tried every imaginable file naming sequence. I tried it including res, the name of my resource folder (both with and without a slash at the beginning), I tried it with a slash preceding images, the folder that contains the image I'm trying to access, and simply the name of the image, again, with and without the slash.

I used Eclipse's "Build Path" to create my res folder in the Project folder, but if I look at the .zip, it doesn't contain the folders I included, even the src folder, but it does contain all of the class files from the src folder. I'm confused why this is happening.

I would really like some insight on what exactly I am doing wrong here.

EDIT: After trying nearly everything imaginable, I finally figured it out. The folder needs to be created as a Package, and the image needs to be placed within the package. A source folder or normal folder doesn't seem to be picked up by the compiler. Just add a package to your source folder for the image to be loaded.

jlars789
  • 155
  • 9
  • `ImageLoader` is a system class? Use one of your own classes, the resource paths are different and you need to refer to *your* app, not the system resources. EDIT: I mean for this line here: `image = ImageIO.read(ImageLoader.class.getResource(uri));` EDIT 2: and you probably have to use an absolute resource path: `"/images/testSprite.png"` Use a "/" to start the path. – markspace Jan 27 '19 at 04:29
  • @markspace ImageLoader is a class I made that contains the getImage() method. Like I said, I tried nearly every type of pathing. That is not the issue from what I can see. I have tried it with the preceding slash and without the preceding slash at least 5 times now. – jlars789 Jan 27 '19 at 04:32
  • Are you sure? Please rename, it, there's an Oracle library with the same name, it might be referenced by Eclipse. https://docs.oracle.com/cd/E29049_01/apirefs.1112/e17487/oracle/jbo/ui/main/images/ImageLoader.html – markspace Jan 27 '19 at 04:33
  • `if I look at the .zip, it doesn't contain the folders I included,` This is bad. You mean the .jar file you build for distribution? It needs to contain the files under /image or nothing will work. Where to you put your images (and other resources) relative to the build path. (I'm not an expert on Eclipse so there might be something I'm missing, but this is how other IDEs work.) – markspace Jan 27 '19 at 04:35
  • @markspace I changed it to ResourceLoader, and the same error is occuring. – jlars789 Jan 27 '19 at 04:36
  • Yes, because there's no chance that collides either. https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/ResourceLoader.html Please use something like `JLarsImageLoader`. I'm sure that's OK. – markspace Jan 27 '19 at 04:37
  • @markspace I put them all relative to the project folder. I even tried putting a res folder in src, and that didn't work. Changing the class name to that did nothing, as well. – jlars789 Jan 27 '19 at 04:38
  • I'm not an eclipse expert, please try this: https://stackoverflow.com/questions/27934796/how-do-i-add-a-resources-folder-to-my-java-project-in-eclipse Verify that the resources end up in the correct place (the .Jar file you build?). Until they do nothing else can possibly work. – markspace Jan 27 '19 at 04:39
  • @markspace As I mentioned in my post, I have **scoured** this site looking for answers before posting. I came across this one, and used the comment explaining how to build the path earlier today to build the path I'm currently using. I made this post for a reason, because it seems like I'm doing something specifically wrong, or my computer is different for some reason, but I'm 99.99% sure it's the former. I appreciate the help, but I do not want to reread a StackOverflow post I have read at least 3 times now. – jlars789 Jan 27 '19 at 04:48

0 Answers0