Question

I have two h:selectOneMenu (1: Countries, 2: Cities). I need to load all cities from one selected country in the cities selectOneMenu using ajax. When I change the value of the countries selectOneMenu my cities selectOneMenu takes a null value from countryBean.selectedCountry.

<h:panelGrid columns="2">
    <h:outputLabel for="countries" value="Countries: " />
    <h:selectOneMenu converter="omnifaces.SelectItemsConverter"
        id="countries" required="true" value="#{countryBean.selectedCountry}">
        <f:selectItem itemLabel="Choose country" />
        <f:selectItems value="#{countriesBB.findAllCountries()}"
            var="country" itemLabel="#{country.name}" />
        <f:ajax event="change" render="cities" />
    </h:selectOneMenu>

    <h:outputLabel for="cities"
        value="Cities: " />
    <h:selectOneMenu converter="omnifaces.SelectItemsConverter"
        id="cities" required="true"
        value="#{cityBean.selectedCity}">
        <f:selectItem itemLabel="Choose city" />
        <f:selectItems value="#{cityBean.findAllCitiesByCountry(countryBean.selectedCountry)}"
            var="city" itemLabel="#{city.name}" />
    </h:selectOneMenu>
</h:panelGrid>

This is the method that find the cities:

public List<city> findAllCitiesByCountry(Country country) {

        List<City> cities = null;
        try {

            cities = citiesService.findAllCitiesByCountry(country);

        } catch (Exception exception) {
            logger.debug("Error finding cities.", exception);
        }

        return cities;

    }

I am getting a NullPointerException because countryBean.selectedCountry is always null. What is the right way to do this?

Was it helpful?

Solution

One of the many rules which a JSF starter need to know:

  • Don't do business logic in a getter method.

Once you try to fix that by keeping your getter methods true getter methods (i.e. do not do anything else than just return property;) and performing the business logic in (post)constructor and/or action(listener) methods, then this particular problem shall disappear.

Here's a kickoff example:

<h:selectOneMenu value="#{bean.country}">
    <f:selectItems value="#{bean.countries}" ... />
    <f:ajax listener="#{bean.changeCountry}" render="cities" />
</h:selectOneMenu>
<h:selectOneMenu id="cities" value="#{bean.city}">
    <f:selectItems value="#{bean.cities}" ... />
</h:selectOneMenu>

With something like this in a @ViewScoped bean:

private Country country; // +getter+setter
private City city; // +getter+setter
private List<Countries> countries; // +getter
private List<Cities> cities; // +getter

@EJB
private SomeService service;

@PostConstruct
public void init() {
    countries = service.getCountries();
}

public void changeCountry() {
    cities = service.getCities(country);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top