Error while creating bean: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError

java, spring

Solution

The problem occurs because

PropertyEditorRegistrySupport.class.getClassLoader()

is `null`. According to JavaDoc, this happens if the class `PropertyEditorRegistrySupport` has been loaded by the bootstrap class loader instead of the system class loader:

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.

Perhaps your Spring libraries are on the endorsed class path?

Problem

I am facing the following error when I try to create bean using Application Context for the circle class! Circle class implements the `Shape` interface which contains a method `draw()`. Configuration: I am learning spring and have setup a Java Project in eclipse with all the required jars (Spring and Apache commons logging). I have the spring.xml in my classpath in the src folder. Have also tried downloading the latest jars (Spring 4.0.4 release, previously worked with 4.0.0) but now is not working. Cannot think of any solution to fix this. Error: ``` May 11, 2014 6:20:50 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@90832e: startup date [Sun May 11 18:20:50 EDT 2014]; root of context hierarchy May 11, 2014 6:20:50 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [spring.xml] Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor': Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:88) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at com.springApplication.DrawingApplication.main(DrawingApplication.java:16) Caused by: java.lang.ExceptionInInitializerError at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1071) ... 13 more Caused by: java.lang.NullPointerException at org.springframework.beans.PropertyEditorRegistrySupport.<clinit>(PropertyEditorRegistrySupport.java:92) ... 14 more ``` DrawingApplication.java ``` package com.springApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DrawingApplication { public static void main(String[] args) { //The beanFactory reads from an XML file //BeanFactory context = new XmlBeanFactory(new FileSystemResource("spring.xml")); //Application Context provides -- Event notification and more than Bean Factory ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); //Here we pass the id of the bean from the XML given in the above line Shape shape = (Shape)context.getBean("circle"); //Calling the draw method through object local object created using Spring shape.draw(); } } ``` Circle.java ``` package com.springApplication; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class Circle implements Shape{ private Point center; public Point getCenter() { return center; } @Autowired @Qualifier("circleRelated") public void setCenter(Point center) { this.center = center; } @Override public void draw() { System.out.println("Drawing a Circle"); System.out.println("Center Point is: (" + center.getX() + ", " + center.getY() + ")"); } } ``` Point.java ``` package com.springApplication; public class Point { private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } } ``` spring.xml ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="pointA" class="com.springApplication.Point"> <qualifier value="circleRelated" /> <property name="x" value="0"/> <property name="y" value="0"/> </bean> <bean id="PointB" class="com.springApplication.Point"> <property name="x" value="0"/> <property name="y" value="20"/> </bean> <bean id="PointC" class="com.springApplication.Point"> <property name="x" value="-20"/> <property name="y" value="0"/> </bean> <bean id="circle" class="com.springApplication.Circle"> </bean> <!-- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> --> <context:annotation-config/> </beans> ``` Please let me know if anything else is needed. Someone please help ! Sorry about the wrong placing!! @Andrei Stefan List of Jars --------- commons-logging-1.1.3 spring-aop-4.0.4.RELEASE spring-aop-4.0.4.RELEASE-javadoc spring-aop-4.0.4.RELEASE-sources spring-aspects-4.0.4.RELEASE spring-aspects-4.0.4.RELEASE-javadoc spring-aspects-4.0.4.RELEASE-sources spring-beans-4.0.4.RELEASE spring-beans-4.0.4.RELEASE-javadoc spring-beans-4.0.4.RELEASE-sources spring-build-src-4.0.4.RELEASE spring-context-4.0.4.RELEASE spring-context-4.0.4.RELEASE-javadoc spring-context-4.0.4.RELEASE-sources spring-context-support-4.0.4.RELEASE spring-context-support-4.0.4.RELEASE-javadoc spring-context-support-4.0.4.RELEASE-sources spring-core-4.0.4.RELEASE spring-core-4.0.4.RELEASE-javadoc spring-core-4.0.4.RELEASE-sources spring-expression-4.0.4.RELEASE spring-expression-4.0.4.RELEASE-javadoc spring-expression-4.0.4.RELEASE-sources spring-framework-bom-4.0.4.RELEASE spring-framework-bom-4.0.4.RELEASE-javadoc spring-framework-bom-4.0.4.RELEASE-sources spring-instrument-4.0.4.RELEASE spring-instrument-4.0.4.RELEASE-javadoc spring-instrument-4.0.4.RELEASE-sources spring-instrument-tomcat-4.0.4.RELEASE spring-instrument-tomcat-4.0.4.RELEASE-javadoc spring-instrument-tomcat-4.0.4.RELEASE-sources spring-jdbc-4.0.4.RELEASE spring-jdbc-4.0.4.RELEASE-javadoc spring-jdbc-4.0.4.RELEASE-sources spring-jms-4.0.4.RELEASE spring-jms-4.0.4.RELEASE-javadoc spring-jms-4.0.4.RELEASE-sources spring-messaging-4.0.4.RELEASE spring-messaging-4.0.4.RELEASE-javadoc spring-messaging-4.0.4.RELEASE-sources spring-orm-4.0.4.RELEASE spring-orm-4.0.4.RELEASE-javadoc spring-orm-4.0.4.RELEASE-sources spring-oxm-4.0.4.RELEASE spring-oxm-4.0.4.RELEASE-javadoc spring-oxm-4.0.4.RELEASE-sources spring-test-4.0.4.RELEASE spring-test-4.0.4.RELEASE-javadoc spring-test-4.0.4.RELEASE-sources spring-tx-4.0.4.RELEASE spring-tx-4.0.4.RELEASE-javadoc spring-tx-4.0.4.RELEASE-sources spring-web-4.0.4.RELEASE spring-web-4.0.4.RELEASE-javadoc spring-web-4.0.4.RELEASE-sources spring-webmvc-4.0.4.RELEASE spring-webmvc-4.0.4.RELEASE-javadoc spring-webmvc-4.0.4.RELEASE-sources spring-webmvc-portlet-4.0.4.RELEASE spring-webmvc-portlet-4.0.4.RELEASE-javadoc spring-webmvc-portlet-4.0.4.RELEASE-sources spring-websocket-4.0.4.RELEASE spring-websocket-4.0.4.RELEASE-javadoc spring-websocket-4.0.4.RELEASE-sources All of these plus other default system libraries are referenced.

Original source