【问题标题】:addition to Managed Bean Best Practice除了托管 Bean 最佳实践
【发布时间】:2014-07-14 21:40:22
【问题描述】:

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

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()) 有一个编译器错误“空指针访问变量 ap 此时只能为空” setAppRepID 的代码和setHelpRepID 相同,并且 setHelpRepID 或 setRuleRepID 都没有编译器错误。我不确定问题是否出在设置 AppProperties ap = null 试图创建 AppProperties ap = new AppProperties 但它不喜欢那样。我想我真的很接近完成这项工作,但是....

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

【问题讨论】:

    标签: java xpages


    【解决方案1】:

    编译器错误是正确的,此时你的变量 ap 只能为空。 遵循来自

    的每个语句
    AppProperties ap = null;
    

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

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

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

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

    public AppProperties() {};
    

    然后尝试改变

    AppProperties ap = null;
    

    AppProperties ap = new AppProperties();
    

    【讨论】:

    • 感谢 Cameron 就是这样 - 在我的 AppProperties 类中需要空构造函数,这样我就可以创建一个新的 AppProperties 实例。现在它可以正确编译,所以现在我可以开始测试它了。再次感谢
    【解决方案2】:

    绝对是“AppProperties ap = null”行。当您说您尝试了“AppProperties ap = new AppProprties”时,是否在末尾包含“()”(即“AppProperties ap = new AppProperties()”)?看起来您的 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多