除了托管 Bean 最佳实践 [英] addition to Managed Bean Best Practice

查看:31
本文介绍了除了托管 Bean 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是(Managed-Bean 最佳实践)的扩展.我编写了一个类 AppProperties 来定义类中的各种项目:

This is an extension to ( Managed-Bean best practice ). I have written a class AppProperties that defines the various items in the 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 中,我有以下内容:

In my bean I have the following:

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 但它不喜欢那样.我想我真的很接近完成这项工作,但是......

everything seems to be ok except at ap.setAppRepID(colVal(2).toString()) there is a compiler error that "Null pointer access The variable ap can only be null at this point" the code for the setAppRepID and setHelpRepID are identical and there is no compiler error of either setHelpRepID or setRuleRepID. I'm not sure if the problem is in the setting AppProperties ap = null tried to create AppProperties ap = new AppProperties but it doesn't like that. I think I'm really close to making this work but ....

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

Thanks to all who have been very patient with me as I climb up the JAVA slope.

推荐答案

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

the compiler error is correct, your variable ap can only be null at that point. Follow each statement from

AppProperties ap = null;

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

并且任何时候都不会将 ap 初始化为一个对象,它仍然是 null.

and at no point is ap initialised to an object, it will still be null.

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

You would also see the compiler error on setHelpRepID or setRuleRepID as well but it doesn't bother showing you because it is already a problem with the first statement. You can try this by commenting out the setAppRepID line and you should see the same error on the next line.

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

make a public constructor in your AppProperties class

public AppProperties() {};

然后尝试改变

AppProperties ap = null;

AppProperties ap = new AppProperties();

这篇关于除了托管 Bean 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆