如何在jsf 2中将arraylist内容添加到菜单中

时间:2015-05-19 12:26:33

标签: jsf-2

我想在jsf页面中添加一个arraylist的内容有meu但是玩我任何东西都可以帮助我!!!! thnks

这是我从bean中的数据库填充我的arraylist的方法

public List<String> profession_pere_list;
public String profession_pere;
//....setters & getters
public  List<String> getSelectProfessions() throws SQLException, NamingException{

    profession_pere_list = new ArrayList<String>();
    initctx = (Context) new InitialContext();
    Context envContext = (Context) initctx.lookup("java:comp/env");
    ds = (DataSource) envContext.lookup("jdbc/reinscription");
    cnx = ds.getConnection();
    state = cnx.createStatement();

    rst = state.executeQuery("select profession from professions");

    while(rst.next())
    {
        etu = new etudiants();
        prof =rst.getString(1);
        etu.setProfession_pere(prof);
        //profession_pere=prof;
        //etu.setProfession_pere(rst.getString(1).toString());

        profession_pere_list.add(prof);

    }

    return profession_pere_list;}

和我的jsf页面:

<h:selectOneMenu value="#{etudiants.profession_pere}">
        <f:selectItems value="#{etudiants.profession_pere_list}"/>

所以我有这个错误未知来源!!!

javax.servlet.ServletException
javax.faces.webapp.FacesServlet.service(Unknown Source)


 cause mère 
java.lang.NullPointerException
    com.sun.faces.renderkit.SelectItemsIterator$GenericObjectSelectItemIterator$GenericObjectSelectItem.updateItem(Unknown Source)
    com.sun.faces.renderkit.SelectItemsIterator$GenericObjectSelectItemIterator$GenericObjectSelectItem.access$600(Unknown Source)
    com.sun.faces.renderkit.SelectItemsIterator$GenericObjectSelectItemIterator.getSelectItemFor(Unknown Source)
    com.sun.faces.renderkit.SelectItemsIterator$IterableItemIterator.next(Unknown Source)
    com.sun.faces.renderkit.SelectItemsIterator$IterableItemIterator.next(Unknown Source)
    com.sun.faces.renderkit.SelectItemsIterator.next(Unknown Source)
    com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(Unknown Source)
    com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(Unknown Source)
    com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(Unknown Source)
    javax.faces.component.UIComponentBase.encodeEnd(Unknown Source)
    javax.faces.component.UIComponent.encodeAll(Unknown Source)
    javax.faces.component.UIComponent.encodeAll(Unknown Source)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(Unknown Source)
    com.sun.faces.application.view.MultiViewHandler.renderView(Unknown Source)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(Unknown Source)
    com.sun.faces.lifecycle.Phase.doPhase(Unknown Source)
    com.sun.faces.lifecycle.LifecycleImpl.render(Unknown Source)
    javax.faces.webapp.FacesServlet.service(Unknown Source)

1 个答案:

答案 0 :(得分:0)

我有同样的问题,rason是由ViewScoped bean中的不同HTTP线程精确修改的List:

@ViewScoped
public class Bean {
    private List<Object> list;

    public List<Object> getList() {
        this.list = new ArrayList<>();
        // code which modifies list
        return this.list;
    }
}

解决方案 - 同步对列表的访问:

    public synchronized List<Object> getList() {

在我的情况下,我有时会遇到异常,有时会出现ConcurrentModificationException。

我还找到了另一种解释:

http://www.adam-bien.com/roller/abien/entry/jsf_nullpointerexception_in_selectitemsiterator_java

当SelectItem标签为空时 - 您也可以获得异常。