除Managed Bean最佳实践外

时间:2014-07-14 21:40:22

标签: java xpages

这是(Managed-Bean best practice)的扩展。我编写了一个AppProperties类,它定义了Class中的各种项目:

public class AppProperties implements Serializable {

    private static final long serialVersionUID = 1L;
        //contents of AppProperties Object
        private String appRepID;
        private String helpRepID;
        private String ruleRepID;
        private String filePath;
        private Vector formNames;
        //end content
        private Session s;
        private String serverName;

        public String getAppRepID() {
            return appRepID;
        }
        public void setAppRepID(String appRepID) {
            this.appRepID = appRepID;
        }
//rest if getters and setters
}

在我的bean中,我有以下内容:

import ca.wfsystems.core.AppProperties;

private final Map<String, AppProperties> internalMap = new HashMap<String, AppProperties>();

    public ApplicationMap() {
        this.buildMap(internalMap);
    }

    private void buildMap(Map<String, AppProperties> theMap) {

        try{
            AppProperties ap = null;
            Session s = ExtLibUtil.getCurrentSession();
            vwApps = s.getCurrentDatabase().getView("vwWFSApplications");
            veCol = vwApps.getAllEntries();
            ve = veCol.getFirstEntry();
            tVE = null;
            while (ve != null){
                Vector colVal = ve.getColumnValues();
                String tAppRepID = colVal.get(2).toString();
                ap.setAppRepID(colVal.get(2).toString());
                ap.setHelpRepID(colVal.get(3).toString());
                ap.setRuleRepID(colVal.get(4).toString());

                theMap.put(colVal.get(0).toString(), ap);
            }
        }catch(Exception e){
            System.out.println(e.toString());
        }finally{
            Utils.recycleObjects(s,vwApps);
        }
    }
除了在ap.setAppRepID(colVal(2).toString())之外,一切似乎都没问题。有一个编译器错误&#34;空指针访问变量ap此时只能为空&#34; setAppRepID和setHelpRepID的代码是相同的,并且setHelpRepID或setRuleRepID没有编译器错误。我不确定问题是否在设置AppProperties ap = null尝试创建AppProperties ap = new AppProperties但它不是那样的。我认为我真的很接近这项工作,但......

感谢所有在我爬上JAVA斜坡时对我很有耐心的人。

2 个答案:

答案 0 :(得分:1)

编译器错误是正确的,您的变量 ap 在此时只能为null。 请遵循

中的每个陈述
AppProperties ap = null;

ap.setAppRepID(colVal.get(2).toString());

并且在任何时候都没有ap初始化为一个对象,它仍然是null。

您还会在setHelpRepID或setRuleRepID上看到编译器错误,但它不会打扰您,因为它已经是第一个语句的问题。你可以通过注释掉setAppRepID行来尝试这个,你应该在下一行看到相同的错误。

在AppProperties类中创建一个公共构造函数

public AppProperties() {};

然后尝试更改

AppProperties ap = null;

AppProperties ap = new AppProperties();

答案 1 :(得分:0)

它肯定是&#34; AppProperties ap = null&#34;线。当你说你试过&#34; AppProperties ap = new AppProprties&#34;时,你是否包括&#34;()&#34;最后(即&#34; AppProperties ap = new AppProperties()&#34;)?看起来您的AppProperties bean具有默认的无参数构造函数,因此应该可以工作。特别是,从您的代码中猜测,我希望您能够在打开while循环之后将该行移动到该位置。

你还有一个等待的无限循环:你永远不会在while循环中将条目设置为下一个。如果您不使用OpenNTF Domino API,我建议使用这样的成语:

ViewEntry entry = veCol.getFirstEntry();
while(entry != null) {
    Vector<?> colVal = ve.getColumnValues();
    ...

    entry.recycle(colVal);
    ViewEntry tempEntry = entry;
    entry = veCol.getNextEntry();
    tempEntry.recycle();
}
相关问题