null bean in selectOneMenu

551 views Asked by At

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);
    }
    //...
}
2

There are 2 answers

5
Safwan Hijazi On BEST ANSWER

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.

4
Jalal Sordo On

You can initialize it in the postconstruct method of the bean.

@PostConstruct
public void init() {
...
    UserInfo uinfo= new UserInfo();
    //you will need to initialize the property datBean of UserInfo in the Constructor.
...
}

Additional info:

@PostConstruct

Target Unreachable

Hope this helps.