Question

I'm trying to make dropdown menu from array, but I always get nullPointerException/. I tried everythin: -on setter I make array with data -on getter if array is null I invoke setter -on getter I tried to make new array object and fill it with setter -Tried to execute dropdown with ajax, and then fill it up with

none of this worekd:/ I have to do it for today, so for now I just hardcoded dropdown, but I want to know where I made mistake:/

here's the drop down code from page:

<h:selectOneMenu id="dataRejestracjiMiesiac" value="#{searchBean.dataRejMiesiac}">
<f:selectItems value="#{searchBean.listaMiesiecy}" var="m" itemLabel="#{m.miesiac}" itemValue="#{m.miesiacID}"/>
</h:selectOneMenu>

and here' the code from my bean:

protected Miesiac[] listaMiesiecy = new Miesiac[12];

public void setListaMiesiecy() {
        this.listaMiesiecy[0] = new Miesiac(1, "Styczen", 31);
        this.listaMiesiecy[1] = new Miesiac(2, "Luty", 28);
        this.listaMiesiecy[2] = new Miesiac(3, "Marzec", 31);
        this.listaMiesiecy[3] = new Miesiac(4, "Kwiecień", 30);
        this.listaMiesiecy[4] = new Miesiac(5, "Maj", 31);
        this.listaMiesiecy[5] = new Miesiac(6, "Czerwiec", 30);
        this.listaMiesiecy[6] = new Miesiac(7, "Lipiec", 31);
        this.listaMiesiecy[7] = new Miesiac(8, "Sierpień", 31);
        this.listaMiesiecy[8] = new Miesiac(9, "Wrzesień", 30);
        this.listaMiesiecy[9] = new Miesiac(10, "Październik", 31);
        this.listaMiesiecy[10] = new Miesiac(11, "Listopad", 30);
        this.listaMiesiecy[11] = new Miesiac(12, "Grudzień", 31);
    }
public Miesiac[] getListaMiesiecy(){
    listaMiesiecy = new Miesiac[12];
    return this.listaMiesiecy;
}

I get same problem with next dropdown from array - with days, this is with months

Thanks in advance for helping:)

Was it helpful?

Solution

You should not populate the list in the setter method. In fact, for non-input components, the setter method is never invoked. The <f:selectItems> is output-only and never invokes its setter. The setter is only invoked for input components when JSF needs to update the model values with submitted values. For example the value of <h:selectOneMenu> itself.

Instead, you should prepopulate the model in the constructor, or better, the @PostConstruct annotated method of the bean.

E.g.

public class Bean {

    private Miesiac[] listaMiesiecy;

    @PostConstruct
    public void init() {
        this.listaMiesiecy = new Miesiac[12];
        this.listaMiesiecy[0] = new Miesiac(1, "Styczen", 31);
        this.listaMiesiecy[1] = new Miesiac(2, "Luty", 28);
        this.listaMiesiecy[2] = new Miesiac(3, "Marzec", 31);
        this.listaMiesiecy[3] = new Miesiac(4, "Kwiecień", 30);
        this.listaMiesiecy[4] = new Miesiac(5, "Maj", 31);
        this.listaMiesiecy[5] = new Miesiac(6, "Czerwiec", 30);
        this.listaMiesiecy[6] = new Miesiac(7, "Lipiec", 31);
        this.listaMiesiecy[7] = new Miesiac(8, "Sierpień", 31);
        this.listaMiesiecy[8] = new Miesiac(9, "Wrzesień", 30);
        this.listaMiesiecy[9] = new Miesiac(10, "Październik", 31);
        this.listaMiesiecy[10] = new Miesiac(11, "Listopad", 30);
        this.listaMiesiecy[11] = new Miesiac(12, "Grudzień", 31);
    }

    public Miesiac[] getListaMiesiecy(){
        return this.listaMiesiecy;
    }

    // No setter required.
}

The general rule is: do not put anything else in getter other than return property; and in setter other then this.property = property;. You should perform business logic in (post)constructor or (action)listener methods instead. If you need to populate the <f:selectItems> depending on another value during a <f:ajax> event, then use <f:ajax listener> instead.

By the way, if the <f:selectItems value> is that hardcoded, you could also put the property behind the value in a separate application scoped bean, so that it's created only once during application's lifetime instead of repeatedly on every single HTTP request.

See also:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top