0

I have an app that runs fine when I am not attempting to use it as a web start app, but when I convert it to one in Netbeans, it goes belly up. The console is showing a null pointer exception when trying to get a resource that is located in a dependent library. The line of code itself grabs a .png file in that library file and uses it for the window icon. The lines of code that kill it is:

java.net.URL url = ClassLoader.getSystemResource("/com/my/icon/someimage.png");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url); // fails with null pointer exception

If I am not trying to do this as a web start app then I have zero issues. It runs perfectly. Web start = failboat. The code is signed, if that makes any difference. The library file that is being used is one large jar that contains 3 library files I created plus 4 others that are needed which I did not code myself. Do I need to sign the library file as well? Is this possibly an issue in and of itself?

EDIT:

If I take out those lines of code, the web app runs without any issues.

Here is the error:

Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException
    at sun.net.util.URLUtil.getConnectPermission(Unknown Source)
    at sun.awt.SunToolkit.checkPermissions(Unknown Source)
    at sun.awt.SunToolkit.createImage(Unknown Source)
    at DDSC.initComponents(DDSC.java:265)
    at DDSC.<init>(DDSC.java:103)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rabbit Guy
  • 1,840
  • 3
  • 18
  • 28
  • 2
    Any particular reason you're using `getSystemResource` instead of `getResource`? – Powerlord Nov 12 '15 at 14:56
  • Thank you. I was using getSystemResource as it had worked before and I never bothered to even try getResource due to being ignorant to the method. – Rabbit Guy Nov 12 '15 at 15:12
  • Agree with @Powerlord, this is definitely not a system resource and it was pure luck that it worked as a non JWS app. See the [embedded resource info. page](http://stackoverflow.com/tags/embedded-resource/info) for the correct method/approach (basically what Powerlord suggested, with a little more detail..). – Andrew Thompson Nov 13 '15 at 01:59

1 Answers1

1

Try using getClassLoader().getResource() with just the filename.
Full path isn't required and getClassLoader() handles the magic via WebStart & also locally:

String filename = "someimage.png";
Image resultImg = null;    
URL url = getClassLoader().getResource(filename);  
if (url != null)
    resultImg = Toolkit.getDefaultToolkit().getImage(url);
Leet-Falcon
  • 2,107
  • 2
  • 15
  • 23
  • Official Webstart help also available via: https://docs.oracle.com/javase/tutorial/deployment/webstart/index.html – Leet-Falcon Nov 16 '15 at 19:06