服务层EJB空指针异常

时间:2013-08-29 18:21:25

标签: java jsf ejb

我有一个postConstruct方法:

@EJB
private VisitServiceLocal visitService;

@PostConstruct
void init() {
    eventModel = new DefaultScheduleModel();
    visits = getDoctorVisits();
}

方法:

public List<Visit> getDoctorVisits() {
    doctorId = (Integer) session.getAttribute("userId");
    return visitService.findMyVisits(doctorId);
}

VisitService:

@Override
public List<Visit> findMyVisits(Integer myId) {
    return visitDao.findMyVisits(myId);
}

VisitDao:

@Override
public List<Visit> findMyVisits(Integer myId) {
    currentDate = new Date();
    try {
        Query q = em.createQuery("SELECT v FROM Visit v WHERE v.doctorId.id = :myId and v.visitStart > :currentDate");
        q.setParameter("myId", myId);
        q.setParameter("currentDate", currentDate);
        List<Visit> visits = q.getResultList();
        return visits;
    } catch (Exception e) {
        System.out.println("Błąd: " + e.getMessage());
        return null;
    }
}

当我运行我的应用程序时,我有一个错误:

  

com.sun.faces.mgbean.ManagedBeanCreationException:发生错误   在托管bean上执行资源注入visitSecretaryMB at   com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:229)     在com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:105)at   com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)     在com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)at   com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)     在   com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)     在   com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)     在   com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)     在com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:103)     在com.sun.el.parser.AstValue.getValue(AstValue.java:179)at   com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)     在   com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)     在   javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)     在   javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)     在javax.faces.component.UIOutput.getValue(UIOutput.java:169)at   org.primefaces.component.outputlabel.OutputLabelRenderer.encodeEnd(OutputLabelRenderer.java:36)     在   javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)     在   com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)     在   com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)     在   com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)     在   javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)     在   org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:59)     在   org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:47)     在   org.primefaces.component.dialog.DialogRenderer.encodeContent(DialogRenderer.java:172)     在   org.primefaces.component.dialog.DialogRenderer.encodeMarkup(DialogRenderer.java:101)     在   org.primefaces.component.dialog.DialogRenderer.encodeEnd(DialogRenderer.java:43)     在   javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)     在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)     在javax.faces.render.Renderer.encodeChildren(Renderer.java:168)at   javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)     在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)     在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)     在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)     在   com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)     在   com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)     在   com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)     在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)at   com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)     在javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)at   org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)     at pl.ePrzychodnia.filter.FilterLogin.doFilter(FilterLogin.java:49)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)     在   org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)     在   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)     在   org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)     在   com.sun.enterprise.v3.services.impl.ContainerMapper $ AdapterCallable.call(ContainerMapper.java:317)     在   com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)     在   com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)     在   com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)     在   com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)     在   com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)     在   com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)     在   com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)     在   com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)     在   com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)     在   com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)     在   com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)     在com.sun.grizzly.ContextTask.run(ContextTask.java:71)at   com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:532)     在   com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:513)     在java.lang.Thread.run(Thread.java:722)引起:   com.sun.faces.spi.InjectionProviderException:   com.sun.enterprise.container.common.spi.util.InjectionException:   尝试调用生命周期方法void的异常   pl.ePrzychodnia.mb.VisitSecretaryMB.init()at   org.glassfish.faces.integration.GlassFishInjectionProvider.invokePostConstruct(GlassFishInjectionProvider.java:231)     在   com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:223)     ... 67更多引起:   com.sun.enterprise.container.common.spi.util.InjectionException:   尝试调用生命周期方法void的异常   pl.ePrzychodnia.mb.VisitSecretaryMB.init()at   org.glassfish.faces.integration.GlassFishInjectionProvider.invokeLifecycleMethod(GlassFishInjectionProvider.java:393)     在   org.glassfish.faces.integration.GlassFishInjectionProvider.invokePostConstruct(GlassFishInjectionProvider.java:306)     在   org.glassfish.faces.integration.GlassFishInjectionProvider.invokePostConstruct(GlassFishInjectionProvider.java:229)     ... 68更多引起:java.security.PrivilegedActionException:   java.lang.reflect.InvocationTargetException at   java.security.AccessController.doPrivileged(Native Method)at   org.glassfish.faces.integration.GlassFishInjectionProvider.invokeLifecycleMethod(GlassFishInjectionProvider.java:376)     ... 70更多引起:java.lang.reflect.InvocationTargetException     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:601)at   org.glassfish.faces.integration.GlassFishInjectionProvider $ 2.run(GlassFishInjectionProvider.java:382)     ... 72更多引起:javax.ejb.EJBException at   com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5215)     在   com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5113)     在   com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4901)     在   com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045)     在   com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994)     在   com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)     在   com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89)     在com.sun.proxy。$ Proxy170.findMyVisits(未知来源)at   pl.ePrzychodnia.mb.VisitSecretaryMB.getDoctorVisits(VisitSecretaryMB.java:322)     at pl.ePrzychodnia.mb.VisitSecretaryMB.init(VisitSecretaryMB.java:61)     ... 77更多引起:java.lang.NullPointerException at   pl.ePrzychodnia.dao.serviceClass.VisitService.findMyVisits(VisitService.java:89)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:601)at   org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)     在   org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)     在   com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388)     at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)     在   com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)     在com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)at   com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)     在   com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)     在sun.reflect.GeneratedMethodAccessor116.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:601)at   com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)     在   com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)     在   com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370)     在   com.sun.ejb.containers.BaseContainer .__截距(BaseContainer.java:5360)     在   com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348)     在   com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214)     ... 81更多

为什么我有这个错误。

但是当我将JSF方法服务改为Dao时,一切正常。

方法:

public List<Visit> getDoctorVisits() {
    doctorId = (Integer) session.getAttribute("userId");
    return visitDao.findMyVisits(doctorId);
}

为什么添加服务层会产生此错误?在服务层I中,只会导致dao方法。

2 个答案:

答案 0 :(得分:4)

Caused by: java.lang.NullPointerException
    at pl.ePrzychodnia.dao.serviceClass.VisitService.findMyVisits(VisitService.java:89) at 

你误解了这个问题。问题不在于JSF方面。问题出在EJB方面。 visitService已正确注入,但在visitService内部,visitDao未正确注入。

您没有显示所有代码,但很可能是您的

@Stateless
public class VisitService implements VisitServiceLocal {

    private VisitDao visitDao;

    @Override
    public List<Visit> findMyVisits(Integer myId) {
        return visitDao.findMyVisits(myId);
    }

}

在这种情况下,您还需要将其注入:

@Stateless
public class VisitService implements VisitServiceLocal {

    @EJB
    private VisitDao visitDao;

    @Override
    public List<Visit> findMyVisits(Integer myId) {
        return visitDao.findMyVisits(myId);
    }

}

顺便说一句,在使用JPA时,您似乎是reinventing/duplicating the DAO pattern。这个双层通常没有任何意义。只需将VisitServiceVisitDao合并到一个EJB中即可。

答案 1 :(得分:1)

此异常意味着托管bean创建失败,因为在托管bean的资源注入期间发生了错误。如果您在bean的构造过程中使用@Resource,@ EJB,ManagedProperty等注释,并且在构造该资源期间发生异常,那么您将获得此异常。

但是,在堆栈跟踪的底部,您应该找到在创建该资源期间发生的异常原因的详细信息。修复 相应的原因并且它应该正常工作。

我看不到完整的堆栈跟踪。