Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java throwing NullPointerException when loading Images using getClass().getResource()

Tags:

java

image

jar

I have an Applet I'm trying to embed into a html file after packaging into a jar file.

Images are loaded in the main Applet Class with the following line

return getImage(getClass().getResource("/../content/"+path));

This works when running through eclipse but when I export it to a jar file I get a Null Pointer Exception. I've done some googling and I've tried to use getResourceAsStream with the following code

InputStream is = new BufferedInputStream(
                 getClass().getResourceAsStream("/../content/"+path));
return ImageIO.read(is);

but this doesn't work through eclipse.

Content is the top level directory of the images and path is the specified path to the required image. The applet is in a folder called game, which is at the same level as content

like image 520
CNevin561 Avatar asked Aug 29 '26 01:08

CNevin561


1 Answers

It was a comment now i answered it here (as OP said it did help): try putting the content into the same package as the class thats accessing the content and change the paths accordingly and see if that helps. If it does, its a problem with your paths.

like image 181
David Kroukamp Avatar answered Aug 31 '26 15:08

David Kroukamp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!