开发者

ejbFacade is null

I call the managed开发者_如何学JAVABean 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?


@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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜