2

I have a session bean

<managed-bean>
  <managed-bean-name>vdcAddBean</managed-bean-name>
  <managed-bean-class>com.cloud.appsportfolio.jsf.vdc.beans.VDCAddBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Now, I am injecting this bean into a request one:

<managed-bean>
      <managed-bean-name>providerSelectionBean</managed-bean-name>
      <managed-bean-class>com.cloud.appsportfolio.jsf.sourcing.ProviderSelectionBean</managed-bean-class>
      <managed-bean-scope>request</managed-bean-scope>
      <managed-property>
        <property-name>vdcAddBean</property-name>
        <property-class>com.cloud.appsportfolio.jsf.vdc.beans.VDCAddBean</property-class>
        <value>#{sessionScope.vdcAddBean}</value> 
      </managed-property>
</managed-bean>

Well, when I'm accessing vdcAddBean in providerSelectionBean java code, I receive a NPE because vdcAddBean is not yet initialized. If I'm going first in my menu, in a page which has vdcAddBean in the back-end and comes back to providerSelectionBean all works great because it seems that vdcAddBean was already initialized.

The question is: how I can force vdcAddBean to be initialized (if it's null) when accessing providerSelectionBean bean?

Thanks.

Cristian Boariu
  • 9,603
  • 14
  • 91
  • 162
  • Are you trying to access vdcAddBean in the Constructor of providerSelectionBean and getting a null pointer? If so you can probably move the logic that depends on vdcAddBean into the setVdcAddBean() method and avoid the NP. – Dave Maple May 18 '11 at 11:38

2 Answers2

2

Replace

<value>#{sessionScope.vdcAddBean}</value> 

by

<value>#{vdcAddBean}</value> 

to get JSF to autocreate the bean.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

JSF managed session beans are stored within the ExternalContext, you can retrieve a map with all of them using the following method, getSessionMap.

The key to this map should be the managed-bean-name, so perhaps you can check for null and if so then try instantiating your bean and putting it directly into the sessionMap?

maple_shaft
  • 10,435
  • 6
  • 46
  • 74