1

My Java application was working fine with Java 8 but now with Java 10 is failing due to this line

setIconImage(Toolkit.getDefaultToolkit().createImage(Icon.class.getResource("/icon/songkong20.png")));

The image referred to exists and is displayed when using Java 8.

This causes this exception

java.lang.NullPointerException
    at java.desktop/sun.awt.image.URLImageSource.getConnection(URLImageSource.java:101)
    at java.desktop/sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:111)
    at java.desktop/sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:263)
    at java.desktop/sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:212)
    at java.desktop/sun.awt.image.ImageFetcher.run(ImageFetcher.java:176)
Naman
  • 27,789
  • 26
  • 218
  • 353
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • is this the complete stacktrace that you get to see? any relevant line of code? how did you start your application? – Naman Aug 13 '18 at 13:40
  • @nullpointer that is complete stacktrace but I have now found the offending line, whats wrong with it. – Paul Taylor Aug 13 '18 at 13:45
  • Ah vaguely remembered something, if I replace Icon.class with one of my own classes, SongKong.class then it works – Paul Taylor Aug 13 '18 at 13:48
  • 1
    `Class.getResource` has been revised in Java9, [here you can read](https://docs.oracle.com/javase/10/docs/api/java/lang/Class.html#getResource(java.lang.String)) more about the impact of the module's classloader and how the resources are found. Better would be to set a debug point within `Class.getResource` method and stepping down with both your use cases(`Icon.class` vs `SongKong.class`) and you'll be able to see the varied resolution by yourself. – Naman Aug 13 '18 at 13:55
  • 1
    Few related links to this(possible duplicates), could be [one](https://stackoverflow.com/questions/48768879/how-to-access-resource-using-class-loader-in-java-9), [two](https://stackoverflow.com/questions/46861589/java-9-jigsaw-accessing-resource-files-from-external-modules) and [three](https://stackoverflow.com/questions/47613602/java-9-0-classloadergetresourceasstream-nullpointerexception) – Naman Aug 13 '18 at 14:00
  • I would say they are not duplicates because the starting point was that I had an exception but didn't show me what part of my code was causing the issue so this question is useful to help others encountering same exception – Paul Taylor Aug 13 '18 at 14:09
  • I haven't voted to close this as a duplicate either, but I am very much bent towards the thought that more the information you share to make the question an appropriate [MCVE](https://stackoverflow.com/help/mcve), the higher its chances of ending up being a duplicate. :) Anyway, the core point is that, by the present code shared, you seem to be accessing the resource of a different module to that of the class in use for `getResource` call. Am I correct in inferring the same? – Naman Aug 13 '18 at 14:15
  • Yes I guess so, and now is all working after changing the class. – Paul Taylor Aug 13 '18 at 14:22

1 Answers1

1

The problem was that I calling getResource() for a file in my module, but calling it on a system class (Icon.class) and this is not allowed from Java 9 onwards.

Changing

Icon.class.getResource("/icon/songkong20.png")));

to

SongKong.class.getResource("/icon/songkong20.png")));

fixed the issue.

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • Right, java.awt.Icon is in the java.desktop module and so Icon.class.getResource(...) will attempt to locate the resource in the java.desktop module. You instead what to locate the resource in your own module or at least in the same JAR file as your code on the class path so changing it to your own class is the right thing to do. – Alan Bateman Aug 27 '18 at 11:43