gpt4 book ai didi

java - facescontext.getcurrentinstance 返回 nullpointerexception

转载 作者:行者123 更新时间:2023-11-30 09:51:48 25 4
gpt4 key购买 nike

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

这是我的 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);

当我将这段代码从构造函数移到方法时,问题就解决了。谁能预测这是为什么?

最佳答案

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

关于java - facescontext.getcurrentinstance 返回 nullpointerexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4516660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com