1

This will no doubt be marked as a duplicate but I figured I'd try anyway. I've looked at all the related questions that have been asked, and I've searched online, and I can't seem to find the solution to this issue.
I am trying to use the Java FX diffuseMap to texture a Box. However, when using

PhongMaterial material = new PhongMaterial();
material.setDiffuseMap(new Image(getClass().getResourceAsStream("/Eternity/Images/ice.png")));
b.setMaterial(material);

I get a NullPointerException;

java.lang.NullPointerException: Input stream must not be null

I have tried messing with the filename, paths, different ways of accomplishing the same task (all of which still involved InputStreams) and even copying and pasting the actual file path (using Intellij's Copy File Path button) but I can't seem to solve this issue. It just doesn't seem to have any effect on the error.

Here's a look at the project hierarchy, barring the main project folder. project hierarchy

Any and all help would be greatly appreciated!

Tobias Wilfert
  • 919
  • 3
  • 14
  • 26
Lloyd7113
  • 187
  • 3
  • 12

2 Answers2

0

Class.getResourceAsStream() uses the class loader, to load a resource from the classpath. So, for your code to work, the directory containing the Eternity directory should be in the classpath. Since it's not, the resource can't be found by the class loader, and null is returned, as documented.

So, either change the classpath of your running program, or move the images to the source folder, so that your IDE (and, hopefully, your build/packaging tool), copies the images to the directory where the class files are compiled. And then use the right path of course (i.e. the full package name, but with slashes instead of dots, and with a starting slash). If the image is in src/foo/bar/ice.png, the package is foo.bar, and the path to use is thus /foo/bar/ice.png.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • So, if my image is in src/Images/ice.png, then I would use that path, right? Is the image automatically contained in some sort of package already? – Lloyd7113 Dec 08 '18 at 13:29
  • No. Read my answer. It has an example. The path would be "/Images/ice.png". Since the file is under the directory Images, which is directly in your source root, its package is Images. Just like if a .java source file was located there, its package would be Images. – JB Nizet Dec 08 '18 at 13:44
  • @vbquestions look into your "out" folder: this is where your program actually runs from. this is where getResource (and getResourceAsStream) is looking for files. – turikhay Dec 09 '18 at 11:02
0

Seems like you're using Intellij, just drag the images directory and drop it in the src directory and rename the path to

... .getResourceAsStream("/Images/ice.png")));

@JB Nizet did a good job of explaining why it doesn't work.

My solution is a temp-fix the right way would be to correctly configure a resource directory and then put your resources there. I suggest you go through Intellij Modules

Ryotsu
  • 786
  • 6
  • 16