Search code examples
jsfmanaged-bean

null bean in selectOneMenu


So I have a selectOneMenu in my user profile page. The value it targets is a field in my user profile table. That field is a foreign key of another table. Initially this value is null because the user hasn't set his info yet.

The select one menu looks like this :

<p:selectOneMenu value="#{userProfileEdit.uinfo.datBean}" >  <!-- This Value is null initially -->
    <f:selectItem itemLabel="Select One" itemValue="#{null}" />
    <f:selectItems value="#{datBeanConverter.beansList}" var="bean" itemLabel="#{bean.title}" itemValue="#{bean}"/>
</p:selectOneMenu>

The value is initially null and I'm getting this error :

value="#{userProfileEdit.uinfo.datBean}": Target Unreachable, 'null' returned null

How can I go around this if I can?

Edit: My uinfo property bean is initialized in the userProfileEdit like so

@ManagedBean
@RequestScoped
public class UserProfileEdit implements Serializable {
    private Userinfo uinfo;
    @EJB
    private UserProfileService us;
    //...

    @PostConstruct
    public void init() {
        setUser();
        this.uinfo = us.getUserInfo(username);
    }
    //...
}

Solution

  • define new datBean variable and put it in value attribute like this

    <p:selectOneMenu value="#{userProfileEdit.datBean}" >  <!-- This Value is null initially -->
           <f:selectItem itemLabel="Select One" itemValue="#{null}" />
           <f:selectItems value="#{datBeanConverter.beansList}" var="bean" itemLabel="#{bean.title}" itemValue="#{bean}"/>
       </p:selectOneMenu>
    

    Then after submit form you can create uinfo object and use datBean object.