0

I am trying to get from selectOneMenu an object that has fields such as first name, last name and so on. This is my form:

<h:form>

<p:outputLabel value="Persons: " />
<p:selectOneMenu value="#{personBean.person}">
    <f:selectItem itemLabel="Select Person" itemValue="" noSelectionOption="true"/>
    <f:selectItems itemLabel="#{person.firstName}" itemValue="#{person}" var="person" value="#{personBean.persons}" />

</p:selectOneMenu>
<br /><br />
<p:commandButton value="Submit"
    action="#{personBean.showSomething()}" icon="ui-icon-check" />


I don't understand.. where am i going wrong? How can i get that object?
I've been trying for a few days but i haven't managed to fix this problem... I tried using a Converter but i kept getting NPEs.

EDIT
This is my converter:(I am trying to get the object from my postgres db with the help of my DAO)

@Named
@FacesConverter(forClass=Person.class)
public class PersonConverter implements Converter {

    @Inject
    private PersonDao personDao;

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent component, String submittedValue) {
        if (submittedValue == null || submittedValue.isEmpty()) {
            return null;
        }

        try {
            Person p = personDao.findById(Long.valueOf(submittedValue));
            return p;
        } catch (NumberFormatException e) {
            throw new ConverterException(new FacesMessage(submittedValue + " is not a valid Person ID"), e);
        }
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
        if (arg2 == null) {
            return "";
        }

        if (arg2 instanceof Person) {
            return String.valueOf(((Person) arg2).getId());
        } else {
            throw new ConverterException(new FacesMessage(arg2 + " is not a valid Person"));
        }
    }

}

This is where i get a NPE Person p = personDao.findById(Long.valueOf(submittedValue));
The find() method works anywhere else... When i used the debugger i noticed that personDao is null. How can i fix this?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
ffs
  • 138
  • 1
  • 1
  • 10
  • Only when i was using the converter. When not, `personBean.showInfo()` was not even called so it didn't return anything. – ffs Aug 19 '15 at 19:46
  • please add more information. like your stacktrace where you get your NPE. Also, shouldn't itemValue be #{person}? instead of the #{person.firstName}? – Fritz Aug 20 '15 at 02:07
  • Thank you for pointing that out! – ffs Aug 20 '15 at 11:14

1 Answers1

0
  • In the page:

            <p:selectOneMenu value="#{personBean.person}">
                <f:selectItem itemLabel="Select Person" itemValue="" noSelectionOption="true"/>
                <f:selectItems itemLabel="#{person.firstName}" itemValue="#{person}" var="person" value="#{personBean.persons}" />
                <f:converter binding="#{personConverter}" />
                <f:attribute name="attrpersons" value="#{personBean.persons}" />                    
            </p:selectOneMenu>
    
  • In the Person bean:

    You must implement toString and equals methods.

  • And finally you have to add the converter class:

    @Named
    public class PersonConverter implements Converter{
    
        public PersonConverter(){}
    
        @Override
          public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
              List<Person> persons = (List<Person>)component.getAttributes().get("attrpersons");
            if (submittedValue.trim().equals("")) {
                return null;
            } else {
                Iterator<Person> it = persons.iterator();
                while(it.hasNext()){
                    Person p = it.next();
                    if(p.toString().equals(submittedValue))
                        return p;                
                }
            }
            return null;
        }
    
        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
            if (value == null) {
                return "";
            } else {
                return value.toString();
            }
        }
    }
    
Javier Haro
  • 1,255
  • 9
  • 14