ejbFacade is null

╄→гoц情女王★ 提交于 2019-12-20 02:54:22

问题


I call the managedBean OverzichtAlle.java from the jsf page overzichtAlleGroepen.xhtml

But when I get on this page i get the errormessage can't instantiate managedBeans.OverzichtAlle due to a Nullpointerexception...

When I debug, I see that my ejbFacade is null..

this is the EJB

@EJB private ProjecttypeEFacade ejbFacade;

and this is my constructor:

public OverzichtAlle() 
{
    projE = ejbFacade.findAll();
    omvormenProjectTypes();
}

projE is a List (entity-list)

What am i doing wrong?


回答1:


@EJBs are injected after bean's construction. It's for the EJB injection manager namely not possible to call a bean setter method before constructing it:

overzichtAlle.setEjbFacade(ejbFacade);
OverzichtAlle overzichtAlle = new OverzichtAlle();

Instead, the following is happening behind the scenes:

OverzichtAlle overzichtAlle = new OverzichtAlle();
overzichtAlle.setEjbFacade(ejbFacade);

So the ejbFacade is not available inside bean's constructor. The normal approach is to use a @PostConstruct method for this.

@PostConstruct
public void init() {
    projE = ejbFacade.findAll();
    omvormenProjectTypes();
}

A @PostConstruct method is called directly after bean's construction and all managed property and dependency injections. You can do your EJB-dependent initializing job in there. The following will then happen behind the scenes:

OverzichtAlle overzichtAlle = new OverzichtAlle();
overzichtAlle.setEjbFacade(ejbFacade);
overzichtAlle.init();

Note that the method name doesn't matter. But init() is pretty self-documenting.



来源:https://stackoverflow.com/questions/7038919/ejbfacade-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!