【问题标题】:facescontext.getcurrentinstance returns nullpointerexceptionfacescontext.getcurrentinstance 返回空指针异常
【发布时间】:2010-12-23 07:22:57
【问题描述】:

我正在创建一个基于 Spring 的 JSF 应用程序,其中我得到 FacesContext.getCurrentInstance,它返回 null。

这是我的 Java 代码

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

这是我的错误的堆栈跟踪

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 

因此由于此错误,Eclipse 中的服务器控制台报告应用程序由于先前的错误而无法启动

我的 web.xml 文件

    <?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>

我不清楚这个错误是属于 Spring 还是 JSF 还是 Eclipse。

我正在使用 Eclipse Galileo、JSF 1.2、Spring 3

请帮忙 提前致谢

更新

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

application-context.xml 中的这些行会导致问题。

更新

这是 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");
}
}

更新

SpringApplicationContextUtil 代码中的这段代码导致问题

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

当我将此代码从构造函数移动到方法时,问题就解决了。谁能预测一下原因?

【问题讨论】:

    标签: java spring jsf


    【解决方案1】:

    我的猜测是你的问题出在你的 spring 配置中:所有的错误都是 spring 框架抛出的,错误代码说你的 ContextInitialized 事件有问题,并且你在 web.xml 中分配了一个 ContextLoadedListener。我会开始在那里寻找原因。

    【讨论】:

    • 感谢您查看我的问题。实际上这昨天工作得很好。但我不知道为什么今天失败了。
    • 嗯,从那以后你有什么改变吗?
    • 这就是问题所在。我不记得究竟是什么变化导致了这个问题。 Eclipse 中有没有办法恢复到前一天的代码?
    • 您可以直接从 eclipse 中使用 CVS、SVN 等,但您必须在需要之前设置版本控制。尝试注释掉您从昨天开始添加的一些功能。也许这将有助于追查问题。
    • @weltraumpirat +1 你是正确的,我在上面发布的行导致了问题。你能找出原因吗?请检查我的更新
    猜你喜欢
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    相关资源
    最近更新 更多