SelectOneMenu Problem

UI Components for JSF
Post Reply
daniel.camargo
Posts: 24
Joined: 22 Oct 2010, 20:00

14 Apr 2011, 13:14

Hi,

Primefaces 3.0.M1 is really nice!

I've had only a small problem with the SelectOneMenu component (p:selectOneMenu).

I created a list of SelectItems and the first item on the list has a null value as shown below:

Code: Select all

List<SelectItem> items = new ArrayList<SelectItem>();
items.add(new SelectItem(null,  " ")); // <-- HERE is the problem
for (Enum e : type.getEnumConstants()) {
	items.add(new SelectItem(e,  e.name()));
}
I had a NullPointerException here:

Code: Select all

public String getSelectedLabel(FacesContext context, SelectOneMenu menu, List<SelectItem> items) {
		Object value = menu.getValue();
        String label = "&nbsp;";    //null display

        if(value == null) {
            if(!items.isEmpty())
                label = items.get(0).getLabel();
            else
                label = "&nbsp;";
        } else {
            for(SelectItem item : items) {
                if(item.getValue().equals(value)) { \\ <-- HERE I get a NullPointerException
                    label = item.getLabel();
                    break;
                }
            }
        }

        return label;
}
Is this really abnormal or I should use another way to get a blank option as the first option on the menu?
JSF2 - Primefaces-3.1.RC1

healeyb
Posts: 365
Joined: 07 Apr 2010, 16:05

14 Apr 2011, 13:26

Hi, you can use the noSelectionOption with the appropriate text, but this
doesn't get selected, it's not totally clear if this is what you want:

Code: Select all

<h:selectOneMenu value="#{...}"
  <f:selectItem itemLabel=" - Select One - "noSelectionOption="true"/>
  <f:selectItems value="#{...}"/>
</h:selectOneMenu>
Regards,
Brendan.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 30 guests