1
votes

I have a postConstruct method:

@EJB
private VisitServiceLocal visitService;

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

Method:

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;
    }
}

When I run my application i have a errors:

com.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing resource injection on managed bean visitSecretaryMB at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:229) at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:105) at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409) at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269) at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244) at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116) at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:103) at com.sun.el.parser.AstValue.getValue(AstValue.java:179) at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224) at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) at javax.faces.component.UIOutput.getValue(UIOutput.java:169) at org.primefaces.component.outputlabel.OutputLabelRenderer.encodeEnd(OutputLabelRenderer.java:36) at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875) at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312) at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185) at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129) at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845) at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:59) at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:47) at org.primefaces.component.dialog.DialogRenderer.encodeContent(DialogRenderer.java:172) at org.primefaces.component.dialog.DialogRenderer.encodeMarkup(DialogRenderer.java:101) at org.primefaces.component.dialog.DialogRenderer.encodeEnd(DialogRenderer.java:43) at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764) at javax.faces.render.Renderer.encodeChildren(Renderer.java:168) at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760) at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402) at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) at pl.ePrzychodnia.filter.FilterLogin.doFilter(FilterLogin.java:49) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:722) Caused by: com.sun.faces.spi.InjectionProviderException: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting invoke lifecycle method void pl.ePrzychodnia.mb.VisitSecretaryMB.init() at org.glassfish.faces.integration.GlassFishInjectionProvider.invokePostConstruct(GlassFishInjectionProvider.java:231) at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:223) ... 67 more Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting invoke lifecycle method void pl.ePrzychodnia.mb.VisitSecretaryMB.init() at org.glassfish.faces.integration.GlassFishInjectionProvider.invokeLifecycleMethod(GlassFishInjectionProvider.java:393) at org.glassfish.faces.integration.GlassFishInjectionProvider.invokePostConstruct(GlassFishInjectionProvider.java:306) at org.glassfish.faces.integration.GlassFishInjectionProvider.invokePostConstruct(GlassFishInjectionProvider.java:229) ... 68 more Caused by: 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 more Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.glassfish.faces.integration.GlassFishInjectionProvider$2.run(GlassFishInjectionProvider.java:382) ... 72 more Caused by: javax.ejb.EJBException at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5215) at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5113) at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4901) at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045) at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222) at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89) at com.sun.proxy.$Proxy170.findMyVisits(Unknown Source) at pl.ePrzychodnia.mb.VisitSecretaryMB.getDoctorVisits(VisitSecretaryMB.java:322) at pl.ePrzychodnia.mb.VisitSecretaryMB.init(VisitSecretaryMB.java:61) ... 77 more Caused by: 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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388) at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162) at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144) at sun.reflect.GeneratedMethodAccessor116.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370) at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360) at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214) ... 81 more

Why i have this error.

But when i changed JSF method Service to Dao all works fine.

Method:

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

Why addition service layer generate this error? In service layer I only causes dao method.

2
In the future questions, please use code tags instead of quote tags to format stack trace!BalusC

2 Answers

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

You misinterpreted the problem. The problem is not in JSF side. The problem is in EJB side. The visitService is properly been injected, however inside the visitService itself, the visitDao is not properly been injected.

You didn't show all the code, but most likely you've a

@Stateless
public class VisitService implements VisitServiceLocal {

    private VisitDao visitDao;

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

}

In that case, you need to inject it there as well:

@Stateless
public class VisitService implements VisitServiceLocal {

    @EJB
    private VisitDao visitDao;

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

}

By the way, you seem to be reinventing/duplicating the DAO pattern while already using JPA. This double layer is generally not making any sense. Just merge VisitService and VisitDao into one EJB.

1
votes

This exception means that the managed bean creation has failed because an error occurred during resource injection of the managed bean. If you are using @Resource, @EJB, ManagedProperty, etc annotation on the bean which is to be injected during bean's construction and an exception occurs during construction of that resource, then you will get this exception.

However, at the bottom of the stacktrace you should find the details of the cause of the exception which occurred during creation of that resource. Fix that cause accordingly and it should work properly.

I could not see the complete stack trace.