When creating ServiceLoader with classloader i.e. ServiceLoader<TestService> s = ServiceLoader.load(Class, ClassLoader),
If the ClassLoader used has no parent classloader , means ClassLoader.parent ==null ,then creation of service loader is working fine but instantiating the providers via stream or iterator breaks with NPE.
For E.g:
s.stream().count() // this one breaks.
for(TestService ts: s) {} // this one breaks too
======================
java.lang.NullPointerException
at java.base/jdk.internal.loader.Loader.getResources(Loader.java:422)
at java.base/jdk.internal.loader.Loader.getResources(Loader.java:422)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.nextProviderClass(ServiceLoader.java:1190)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1215)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.access$1200(ServiceLoader.java:1103)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator$1.run(ServiceLoader.java:1262)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator$1.run(ServiceLoader.java:1261)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1264)
at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1294)
at java.base/java.util.ServiceLoader$ProviderSpliterator.tryAdvance(ServiceLoader.java:1478)
...
======================