dnault / therapi-runtime-javadoc

Read Javadoc comments at run time.
Apache License 2.0
117 stars 19 forks source link

NullPointerException in RuntimeJavadoc due to null ClassLoader #12

Closed joffrey-bion closed 6 years ago

joffrey-bion commented 6 years ago

Here is the stacktrace I got when using Livedoc v4.3.2 which uses therapi-runtime-javadoc 0.4.0:

java.lang.NullPointerException: null
        at com.github.therapi.runtimejavadoc.RuntimeJavadoc.getJavadoc(RuntimeJavadoc.java:41) ~[therapi-runtime-javadoc-0.4.0.jar:na]
        at com.github.therapi.runtimejavadoc.RuntimeJavadoc.getJavadoc(RuntimeJavadoc.java:32) ~[therapi-runtime-javadoc-0.4.0.jar:na]
        at com.github.therapi.runtimejavadoc.RuntimeJavadoc.getJavadoc(RuntimeJavadoc.java:75) ~[therapi-runtime-javadoc-0.4.0.jar:na]

It looks like RuntimeJavadoc crashes at line 41:

try (InputStream is = classLoader.getResourceAsStream(resourceName)) {

My bet is that the classloader is null, which is possible as the doc states:

Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.

One solution would be to directly use the getResourceAsStream() method on the class itself rather than on its ClassLoader. I understand that the getJavadoc(String, ClassLoader) overload is needed for tests, but it should be easy to just extract the code properly to maximize code reuse while avoiding using the classloader overload in production code.

dnault commented 6 years ago

Thanks!