目标无法访问,'null'以JSF格式返回null [重复]

问题描述 投票:1回答:2

我测试了这个源代码:

豆:

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对象的正确方法吗?

PS:

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

javax.el.PropertyNotFoundException: /NewAccount.xhtml @38,126 value="#{NewAccountController.dataList['userid']}": Target Unreachable, 'null' returned null
java jsf jsf-2
2个回答
4
投票

使用上面的代码,NewAccountObj为null。因此,当调用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 getter:

public NewAccountObj getDataList() {
    if(na == null){
        na = new NewAccountObj();
    }
    return na;
}

-1
投票
ADD set property method

public int setUserid(int userid) {
           this.userid =userid ;
        }
© www.soinside.com 2019 - 2024. All rights reserved.