3

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

ftr
  • 2,105
  • 16
  • 29
CNevin561
  • 143
  • 1
  • 3
  • 12
  • 5
    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... – David Kroukamp Jun 17 '12 at 16:25
  • Thought so I see that if the resources are in their own path and package, but not in the current package, it never works out! even when you try go back a directory – David Kroukamp Jun 17 '12 at 16:32
  • 1
    This [thread](http://stackoverflow.com/questions/9864267/load-icon-image-exception/9866659#9866659) might can put more light on the topic :-) – nIcE cOw Jun 18 '12 at 04:07

3 Answers3

3

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.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
0

I had the same issue. But solved it using :-

<ClassName>.class.getClass().getResource(urlString);

Hope this helps...

Robin
  • 109
  • 8
0

You could try this instead.

getClass().getClassLoader().getResourceAsStream(name)

Also, your path looks a bit weird. /.. basically means the parent directory of the root, which cannot possibly work.

Jilles van Gurp
  • 7,927
  • 4
  • 38
  • 46