我會假設下面的代碼是安全的,但是我在調用hasMoreElements()
時得到了一個NPE。任何想法可能是錯誤的?當迭代ClassLoader.getResources()的結果時奇怪的NPE
我應該補充一點,我在64位Windows上使用Java 1.7.0_55-b13。
final List<URL> urls = new ArrayList<URL>();
final String plUri = "META-INF/plugin.xml";
Enumeration<URL> urlsEn =
Thread.currentThread().getContextClassLoader().getResources(pluginsUri);
if (urlsEn != null) {
while (urlsEn.hasMoreElements()) { // NPE happens here
final URL u = urlsEn.nextElement();
urls.add(u);
}
}
堆棧跟蹤:
java.lang.NullPointerException
at sun.misc.MetaIndex.mayContain(MetaIndex.java:243)
at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:830)
at sun.misc.URLClassPath$2.next(URLClassPath.java:273)
at sun.misc.URLClassPath$2.hasMoreElements(URLClassPath.java:283)
at java.lang.ClassLoader$2.hasMoreElements(ClassLoader.java:1322)
at sun.misc.CompoundEnumeration.next(CompoundEnumeration.java:45)
at sun.misc.CompoundEnumeration.hasMoreElements(CompoundEnumeration.java:54)
at sun.misc.CompoundEnumeration.next(CompoundEnumeration.java:45)
at sun.misc.CompoundEnumeration.hasMoreElements(CompoundEnumeration.java:54)
at com.github.jochen.afw.core.guice.GuiceComponentFactoryBuilder.getComponentUrls(GuiceComponentFactoryBuilder.java:256)
at com.github.jochen.afw.core.guice.GuiceComponentFactoryBuilder.build(GuiceComponentFactoryBuilder.java:160)
at com.github.jochen.afw.core.guice.GuiceComponentFactoryBuilderTest.testSuccessfullConfiguration(GuiceComponentFactoryBuilderTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
....
不是'urlsEn'爲空的問題 - 所以它看起來像一個NPE進一步拆毀堆棧bug .. – assylias
什麼是你的線程上下文類加載器的類型? –