Why do I get a NullPointerException when initializing Spring

吃可爱长大的小学妹 提交于 2019-12-01 20:40:29

The cause of problem is -Djava.endorsed.dirs=$PROJECTHOME/target/lib org.springframework.beans.factory.support.DefaultListableBeanFactory contains the following code:

static {
    ClassLoader cl = DefaultListableBeanFactory.class.getClassLoader();
    try {
        javaxInjectProviderClass = cl.loadClass("javax.inject.Provider"); //Line 103
    }
    catch (ClassNotFoundException ex) {
        // JSR-330 API not available - Provider interface simply not supported then.
    }
}

It causes a NullPointerException, because getClassLoader() returns null when class is loaded via -Djava.endorsed.dirs. From javadoc:

Some implementations may use null to represent the bootstrap class loader.

So, use -classpath (with explicit specification of all jars) instead of -Djava.endorsed.dirs

Exception in thread "main" java.lang.ExceptionInInitializerError is occurs while adding User Library. I faced same problem in Hibernate and Spring as well. So i removed User Library say "Spring" then i add jars manually it work perfectly.

coldiron

In Eclipse:

If you are using the spring jars as user library(Say SpringLib), see if the user library for spring is added(or checked) as System Library(added to the boot class path). If yes, remove the check mark.`

Just add the jars to referenced libraries and not to user library. It worked for me!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!