2

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);
    }
    //...
}
Ced
  • 12,657
  • 11
  • 62
  • 124

2 Answers2

2

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.

Safwan Hijazi
  • 2,001
  • 2
  • 14
  • 29
  • I'm not sure what you meant with your second answer (my bean was already instantiated). However the first one works though I had to change something so I'm gonna edit your comment, I hope you don't mind. I'm still perplex why that works and not my original code. – Ced Jun 23 '15 at 02:22
  • I went ahead and deleted the second solution (sorry if that was not correct from my part). I thought you misunderstood my initial problem with it. I could be wrong so sorry if my removal of your answer was incorrect. – Ced Jun 23 '15 at 02:33
  • The problem is uinfo is null, so the solution is to initialize this object or replace, @PostConstruct method is called after constructor, so here you initialize. or use first solution – Safwan Hijazi Jun 23 '15 at 07:34
  • Uinfo is not null !!! Your answer is not correct that's why I edited it. Read my post again. – Ced Jun 23 '15 at 10:32
  • Oh sorry, had a pretty rough night. Cheers – Ced Jun 23 '15 at 10:36
1

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.

Community
  • 1
  • 1
Jalal Sordo
  • 1,428
  • 3
  • 37
  • 64
  • Please accept this question if it helped solving your problem, as this would be appreciated from other users that has the same problem. – Jalal Sordo Jun 22 '15 at 23:37
  • Yes I will, I was away for 2 hours. Ima check this now. – Ced Jun 23 '15 at 00:12
  • great, don't hesitate asking any clarifications. – Jalal Sordo Jun 23 '15 at 00:13
  • I'll need clarifications indeed. My uinfo bean is already instantiated (I added the code in my main post). So I'm not sure if I understood incorrectly what you said. – Ced Jun 23 '15 at 02:15