问题
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:
@EJB
s 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