Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ExceptionInInitializerError Exception when creating Application Context in Spring

I am practicing with Spring, and am getting a java.lang.ExceptionInInitializerError exception when I try to instantiate the context. The Exception appears below, with my code following it. I have simplified my experiment from before.

The Exception

Oct 17, 2012 5:54:22 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@570c16b7: startup date [Wed Oct 17 17:54:22 CDT 2012]; root of context hierarchy
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:195)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:128)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:535)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:449)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at helloworld.HelloWorldTest.main(HelloWorldTest.java:13)
Caused by: java.lang.NullPointerException
at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:105)
... 7 more

My configuration 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:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<bean id="messageContainer" class="helloworld.MessageContainer">
    <property name="message" value="Hello World">
    </property>
</bean>

<bean id="messageOutputService" class="helloworld.MessageOutputService">
</bean>

My test class.

package helloworld;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorldTest {

/**
 * @param args
 */
public static void main(String[] args)
{
    ApplicationContext context = new ClassPathXmlApplicationContext("HelloWorldTest-context.xml");

    MessageContainer message = context.getBean(MessageContainer.class);

    MessageOutputService service = context.getBean(MessageOutputService.class);

    service.outputMessageToConsole(message);

}

}
like image 987
cyotee doge Avatar asked Oct 10 '12 19:10

cyotee doge


1 Answers

Line 17 does not correspond to the context.getBean("userRepository" line, it corresponds to the line before that where you initialize the Spring context. And actually you can also see that through stacktrace, it says it failed on Line 83 of ClassPathXmlApplicationContext where exists the constructor of that class.

Anyway, this exception is generally thrown when Spring can not create any bean for whatever reason (Exceptions in constructors, resource loading issues, class loading issues, etc.). I would suggest decreasing the log level for spring classes and for your own libraries to see what's going on underneath.

org.springframework=TRACE
com.gamemanagertest=TRACE
com.gamemanagertest=TRACE

And also check your resource files whether they're accessible by your application and constructors of all your objects if they produce any errors, etc.

like image 76
melihcelik Avatar answered Sep 23 '22 09:09

melihcelik