我在@PostConstruct - init()方法中使用managedProperty时得到Nullpointer异常,如下所示:
package mx.com.clubjava.pexapp.view.bean;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import mx.com.clubjava.pexapp.model.domain.Estado;
import mx.com.clubjava.pexapp.model.service.EstadoFacade;
@ManagedBean(name = "estadoSearchBean")
@ViewScoped
public class EstadoSearchBean implements Serializable {
private static final long serialVersionUID = -4148160405036136787L;
private List<Estado> estadoList;
@ManagedProperty("#{estadoFacade}")
private EstadoFacade estadoFacade;
@PostConstruct
public void init() {
estadoList = estadoFacade.getEstados();
}
public List<Estado> getEstadoList() {
return estadoList;
}
public void setEstadoList(List<Estado> estadoList) {
this.estadoList = estadoList;
}
public void setEstadoFacade(EstadoFacade estadoFacade) {
this.estadoFacade = estadoFacade;
}
}
EstadoFacade类:
package mx.com.clubjava.pexapp.model.service.impl;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import mx.com.clubjava.pexapp.model.dao.EstadoDao;
import mx.com.clubjava.pexapp.model.domain.Estado;
import mx.com.clubjava.pexapp.model.service.EstadoFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service(value = "estadoFacade")
@Transactional
@Scope( value= "request", proxyMode= ScopedProxyMode.INTERFACES )
public class EstadoFacadeImpl implements EstadoFacade, Serializable{
/**
*
*/
private static final long serialVersionUID = 3354568951955093044L;
private List<Estado> estados;
private EstadoDao estadoDao;
@PostConstruct
private void prepareData() {
estados = estadoDao.getAllActive();
}
@Override
public List<Estado> getEstados() {
return estados;
}
@Override
public Estado getEstadobyId(Long id) {
Estado result = null;
System.out.println("aqui va esto"+estados.size());
for (int i = 0, j = estados.size(); i < j && result == null; i++) {
Estado estado = estados.get(i);
if (estado.getIdEstado() == id){
result = estado;
}
}
return result;
}
@Autowired
public void setEstadoDao(EstadoDao estadoDao) {
this.estadoDao = estadoDao;
}
}
这是错误堆栈跟踪:
java.lang.NullPointerException
mx.com.clubjava.pexapp.view.bean.EstadoSearchBean.init(EstadoSearchBean.java:27)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider.newInstance(Tomcat7AnnotationLifecycleProvider.java:68)
org.apache.myfaces.config.ManagedBeanBuilder.buildManagedBean(ManagedBeanBuilder.java:162)
org.apache.myfaces.el.unified.resolver.ManagedBeanResolver.createManagedBean(ManagedBeanResolver.java:303)
org.apache.myfaces.el.unified.resolver.ManagedBeanResolver.getValue(ManagedBeanResolver.java:266)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:142)
org.apache.myfaces.el.VariableResolverImpl.resolveVariable(VariableResolverImpl.java:65)
org.apache.myfaces.el.convert.VariableResolverToELResolver.getValue(VariableResolverToELResolver.java:96)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:142)
org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:72)
org.apache.el.parser.AstValue.getValue(AstValue.java:161)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
org.apache.myfaces.view.facelets.el.TagValueExpression.getValue(TagValueExpression.java:85)
javax.faces.component._DeltaStateHelper.eval(_DeltaStateHelper.java:243)
javax.faces.component.UISelectItems.getValue(UISelectItems.java:67)
org.richfaces.component.util.SelectUtils$SelectItemsIterator.createUISelectItemsIterator(SelectUtils.java:226)
org.richfaces.component.util.SelectUtils$SelectItemsIterator.createIterator(SelectUtils.java:244)
org.richfaces.component.util.SelectUtils$SelectItemsIterator.computeNext(SelectUtils.java:168)
org.richfaces.component.util.SelectUtils$SelectItemsIterator.computeNext(SelectUtils.java:145)
com.google.common.collect.AbstractIterator.tryToComputeNext(AbstractIterator.java:143)
com.google.common.collect.AbstractIterator.hasNext(AbstractIterator.java:138)
org.richfaces.renderkit.SelectHelper.getConvertedSelectItems(SelectHelper.java:72)
org.richfaces.renderkit.SelectRendererBase.getConvertedSelectItems(SelectRendererBase.java:138)
org.richfaces.renderkit.html.SelectRenderer.doEncodeEnd(SelectRenderer.java:182)
org.richfaces.renderkit.RendererBase.encodeEnd(RendererBase.java:180)
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:519)
org.apache.myfaces.shared_impl.renderkit.RendererUtils.renderChild(RendererUtils.java:543)
org.apache.myfaces.shared_impl.renderkit.html.HtmlGridRendererBase.renderChildren(HtmlGridRendererBase.java:315)
org.apache.myfaces.shared_impl.renderkit.html.HtmlGridRendererBase.encodeEnd(HtmlGridRendererBase.java:150)
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:519)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:618)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:614)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:614)
org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1159)
org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:263)
javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:59)
org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85)
org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
我使用richfaces 4.5.2和spring 3.0.5。
我也尝试使用@Autowire注入EstadoFacade,结果相同,只是不知道为什么注射不能用于PostContructed方法......
任何帮助都会非常感激我已经被困了差不多两天这个isssue =(
的问候。