开发者

facescontext.getcurrentinstance returns nullpointerexception

I am creating a Spring based JSF application, where I am getting FacesContext.getCurrentInstance which returns null.

Here is my Java code

public static ServletContext getServletContext() 
{       
    return (ServletContext) FacesContext.getCurrentInstance()
    .getExternalContext().getContext();
}

This is the stack trace of my error

SEVERE: Exception sending context initialized event to listener instance of class    
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbSettingsServiceTarget' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.smartcall.service.impl.DbSettingsServiceImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:883)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:839)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:519)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate     bean class [com.smartcall.service.impl.DbSettingsServiceImpl]:    Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:115)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:877)
... 31 more
Caused by: java.lang.NullPointerException
at com.smartcall.util.FacesUtil.getServletContext(FacesUtil.java:21)
at com.smartcall.util.SpringApplicationContextUtil.init(SpringApplicationContextUtil.java:21)
at com.smartcall.util.SpringApplicationContextUtil.<init>(SpringApplicationContextUtil.java:16)
at com.smartcall.service.impl.DbSettingsServiceImpl.init(DbSettingsServiceImpl.java:17)
at com.smartcall.service.impl.DbSettingsServiceImpl.<init>(DbSettingsServiceImpl.java:12)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:100)
... 33 more 

and hence due to this error Server console in Eclipse reports that application failed to startup due to previous errors

My web.xml file

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>smartcall2.0</display-name>
  <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.CONFIG_FILES</param-name>
    <param-value>
        /WEB-INF/faces-config.xml,/WEB-INF/faces-managed-bean.xml,/WEB-INF/faces-navigation.xml
    </param-value>
</context-param>

<listener>
    <listener-class>
        com.sun.faces.config.ConfigureListener
    </listener-class>
</listener>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<filter>
    <display-name>RichFaces Filter</display-name>
    <filter-name>richfaces</filter-name>
    <filter-class>org.ajax4jsf.Filter</filter-class>
</filter>

<filter-mapping>
    <filter-name>richfaces</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<context-param>
  <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
  <param-value>.xhtml</param-value>
 </context-param>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

<session-config>
   <session-timeout>180</session-timeout>
</session-config>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

I am not clear whether this error belongs to Spring or JSF or Eclipse.

I am using Eclipse Galileo, JSF开发者_StackOverflow中文版 1.2,Spring 3

Please help Thanks in advance

UPDATE

<!-- dbSettings Service Definition -->
<bean id="dbSettingsServiceTarget"
    class="com.smartcall.service.impl.DbSettingsServiceImpl">
    <property name="dbSettingsDAO">
        <ref local="dbSettingsDAO" />
    </property> 
</bean>

these lines in application-context.xml causes the problem.

UPDATE

This is the code of DbSettingsServiceImpl.

public class DbSettingsServiceImpl implements DbSettingsService
{
private SpringApplicationContextUtil springApplicationContextUtil = null;

private DbSettingsDAO dbSettingsDAO;

public DbSettingsServiceImpl()
{
    init();
}

public void init()
{
    springApplicationContextUtil = new SpringApplicationContextUtil();      
}



public DbSettingsDAO getDbSettingsDAO() {
    return dbSettingsDAO;
}

public void setDbSettingsDAO(DbSettingsDAO dbSettingsDAO) {
    this.dbSettingsDAO = dbSettingsDAO;
}

public String getDatabaseNameFromSettings() 
{

    return springApplicationContextUtil.getDatabaseDetailForThePropery("username");
}

public String getDatabasePasswordFromSettings() 
{   
    return springApplicationContextUtil.getDatabaseDetailForThePropery("password");
}

public String getDatabaseServerNameFromSettings() 
{   
    return springApplicationContextUtil.getDatabaseDetailForThePropery("server");
}

public String getDatabaseUserNameFromSettings() 
{

    return springApplicationContextUtil.getDatabaseDetailForThePropery("username");
}
}

Update

This piece of code in SpringApplicationContextUtil code causing the problem

ServletContext servletContext = FacesUtil.getServletContext();
    this.context = WebApplicationContextUtils
            .getRequiredWebApplicationContext(servletContext);

When I moved this code from constructor to the method the problem solved. Can any one predict why?


My guess would be your problem is in your spring configuration: All the errors are thrown by the spring framework, the error code says you have a problem with the ContextInitialized event, and you have assigned a ContextLoadedListener in your web.xml. I would start looking for the cause there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜