1

我测试了这个源代码:

豆:

private NewAccountObj na;

public class NewAccountObj {

    private int userid;
        ............

    public NewAccountObj(int userid.............) {

        this.userid = userid;
            ............
        }

        public int getUserid() {
            return userid;
        }

        ...............

    }

    // Getters 
    public NewAccountObj getDataList() {        
        return na;
    }

JSF 页面:

<h:panelGrid columns="2">
    <h:panelGroup>User ID</h:panelGroup>
    <h:panelGroup>
        <h:inputText id="userid" value="#{bean.dataList['userid']}">                    
        </h:inputText>
    </h:panelGroup>
......................
</h:panelGrid> 

当我提交表格时,我得到Target Unreachable, 'null' returned null. 你能帮我找出问题吗?Maye 这不是访问 Java 对象的正确方法h:panelGrid吗?

PS:

我在 Glassfish 日志中收到此错误消息:

javax.el.PropertyNotFoundException: /NewAccount.xhtml @38,126 value="#{NewAccountController.dataList['userid']}": Target Unreachable, 'null' returned null
4

2 回答 2

4

使用上面的代码,NewAccountObj 为空。所以当getDataList()被调用时,它返回null。然后它会调用null.getUserId()

na需要初始化。我在您的评论中看到您有一个很长的构造函数,您应该创建另一个没有参数的构造函数(或对象工作所需的最小值)。

private NewAccountObj na;

    public class NewAccountObj {

        private int userid;
        ............

        public NewAccountObj() {
            new New AccountObj(0,0,...........);
        }

        public NewAccountObj(int userid.............) {

            this.userid = userid;
            ............

        }

        public int getUserid() {
            return userid;
        }

        ...............

    }

    // Getters 
    public NewAccountObj getDataList() {        
        return na;
    }

并像这样更改 datalist 吸气剂:

public NewAccountObj getDataList() {
    if(na == null){
        na = new NewAccountObj();
    }
    return na;
}
于 2012-12-14T12:34:38.977 回答
-1
ADD set property method

public int setUserid(int userid) {
           this.userid =userid ;
        }
于 2012-12-13T10:52:49.637 回答