0

JSF SelectOneMenu model value is not set but I don't know what to do.

Here are my code.

header.xhtml

<h:selectOneMenu value="#{bb.loginUser}">
 <f:selectItems value="#{bb.getAllUser()}"/>
 <f:ajax event="change"/>
</h:selectOneMenu>

Bb.java

@Named
@SessionScoped
public class Bb implements Serializable{

@EJB
TimeCardFacade tcDb;
@EJB
UserFacade usrDb;

@Id
private Integer user_id;
private String user_name;

private User loginUser;
public String startWork(){
    String time1 = ***set current time***;
    User usr = loginUser; 
    TimeCard tc = new TimeCard("startWork",time1,usr);
    usr.addTimeCard(tc);
    tcDb.create(tc);
    return "timeCard?faces-redirect=true";
}
public List<User> getAllUser(){
    return usrDb.findAll();
}

***loginUser setter and getter*** 

when I go to the next page, I get NPE error. I did a debug and found out "loginUser" is null.

Can anyone help, please? Thank you.

clo
  • 11
  • 2
  • 1
    *"I did a debug and found out "loginUser" is null."* ... So that's the cause. Now look for the reason that the setter is not being called on that object, or if it is being called ... why it is being called with a `null` argument. Hint: it you expect the value to be injected, check that you have declared it appropriately. – Stephen C Jun 13 '22 at 09:50
  • 1
    You should not tag JSF / CDI question with Java unless you can replicate your question in a main method (which makes it not a JSF question). Please add a [mcve]. – Jasper de Vries Jun 13 '22 at 14:11
  • Indeed. Do not tag [jsf] questions with [java]. They are unlikely to properly understand these. I fixed the dupe target to the proper one. – BalusC Jun 14 '22 at 05:35
  • @StephenC Thank you Stephen. I still don't know where to check. I thought when I select 'loginUser' from select-one-menu, loginUser would be updated, but actually it wasnt happened and 'null'. What do I miss? – clo Jun 14 '22 at 05:36
  • I edited my question ( added code that i didnt write ). Also my web application has db package which has AbstractFacade.java, UserFacade.java, TimeCardFacade.java ( auto generated with NetBeans function ). Thank you. – clo Jun 14 '22 at 05:44

0 Answers0