Error in Java server faces managed form bean

Asked By 140 points N/A Posted on -
qa-featured

Hello techyV experts.

Dependency injection error in Java server faces managed form bean (Java 6 and JSF 2.0)I need help in diagnosing a dependency injection problem. I have a class in my web project that is calling an EJB3 bean.

I am using JSF 2.0 and have a managed bean that is handling the user interface. The managed bean is set as Request Scope. I am getting a null reference exception when i attempt to access the EJB reference in the constructor.

SHARE
Best Answer by TechnoHat
Answered By 0 points N/A #89701

Error in Java server faces managed form bean

qa-featured

Java 6 introduced dependency injection that made life easier for the programmer by reducing the number of code. But it also introduced certain pre-checks that you need to do.

How dependency injection works is that:

1. First creates the base object.
2. Then it scans for injected references inside the base object.
3. Then creates each referred object.
4. Finally it sets the created objects to the respective private variables of the base object.

If you look at the steps, in order to inject the created instances, the base object must exist. If any attempt by the base object to refer to an injected reference before it being initialized, you will get a null reference exception.

Answered By 140 points N/A #89702

Error in Java server faces managed form bean

qa-featured

Are you saying that I cannot call an injected reference in the constructor of my form bean?

I did get a gist of what you explained.

Just to make sure, can you show me with respect to the code?

Answered By 0 points N/A #89703

Error in Java server faces managed form bean

qa-featured

Yes. You cannot access a injection reference before it is initialized. You need to wait till the UIFormBean constructor completes execution.

public class UIFormBean {

@EJB private SkuManagerLocal skuManager; <= this is only initialized after the construction of the UIFormBean

public UIFormBean () {  <= constructor called first

this.skuManager.initialize();  <= you can't call this because it is not yet initialized. The UIFormBean has not been created as yet.

 }

}

Answered By 140 points N/A #89705

Error in Java server faces managed form bean

qa-featured

Now I understand! Thank you TechnoHat! But this puts me in a predicament! I need to call the EJB method prior to any other action in the UIFormbean! How do I call the EJB method in my bean if I cant call it in the constructor?

Best Answer
Best Answer
Answered By 0 points N/A #89706

Error in Java server faces managed form bean

qa-featured

Create a public method in the UIFormBean and put the calling of the injected reference inside it. Then add a dummy JSF control and call the method of the UIFormBean. What I generally do is to create a boolean method signature and bind it to the Form tags "rendered" attribute. Since the form element is the first element after the JSF view element, the "rendered" attribute gets call first.

class UIFormBean {
@EJB private SkuManagerLocal

public UIFormBean() {  // empty }

public Boolean init() {
    this.skuManager.initialize();
  return true;
}

In the JSF page i use:

<h:form rendered="#{uiForm.init}"  ….

Answered By 140 points N/A #89707

Error in Java server faces managed form bean

qa-featured

Just for my understanding,

what if I change the RequestScope to SessionScoped in the managed bean?

Would I still be unable to call the references in the constructor?

I still feel this is a fundamental bug in Java 6!

Answered By 0 points N/A #89709

Error in Java server faces managed form bean

qa-featured

You cannot call the injected reference inside a constructor if you are using dependency injection. Its by design.

There is always a give and a take. If you really need to call a reference inside a constructor,

You need to fallback to the old method of a context lookup.

Answered By 140 points N/A #89710

Error in Java server faces managed form bean

qa-featured

Thank you TechnoHat for taking your time to explain.

I learnt a lot! Thank You! I am sure to bug you in the future!

Answered By 0 points N/A #89711

Error in Java server faces managed form bean

qa-featured

You are most welcome.

I will be at techyV website if you need to! Happy coding!

Related Questions