目标不可达,'null'以 JSF 形式返回空值



我测试了这个源代码:

豆:

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 这不是在 h:panelGrid 中访问 Java 对象的正确方法吗?

附注:

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

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

使用上面的代码,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;
    }

并像这样更改数据列表获取器:

public NewAccountObj getDataList() {
    if(na == null){
        na = new NewAccountObj();
    }
    return na;
}
ADD set property method
public int setUserid(int userid) {
           this.userid =userid ;
        }

最新更新