日期:2014-05-20  浏览次数:20754 次

ActionForm调用service返回空指针问题
我想在ActionForm的reset方法中载入所有的下拉框内容,然后填充到页面上的各个控件中,这个查询用到了Service中的一个查询方法,此查询方法调用Dao,而Dao是通过调用getHibernateTemplate来实现对数据据的访问操作的,结果给返回空指针,此方法在Action中测试过,没有问题!

以下是ActionForm的reset方法中的部分代码:
Java code
try
        {
            ServicesImpl_F service=new ServicesImpl_F();   //调用Service
            List<Department> list=service.getDepartment();   //service中一个返回部门信息列表的方法
            System.out.println("共有"+list.size()+"个部门");
            List<LabelValueBean> deptlists=new ArrayList<LabelValueBean>();
            for(int i=0;i<list.size();i++)
            {
                String bmbh=list.get(i).getBmbh();
                String bmmc=list.get(i).getBmmc();
                deptlists.add(new LabelValueBean(bmbh,bmmc));
            }
            this.setDepts(deptlists);                //depts是一个List集合变量,返回给JSP界面的<html:options>控件 
        }
        catch(Exception e)
        {
            System.out.println(e);
            e.printStackTrace();
        }


------解决方案--------------------
如果你用的spring的话我感觉这里可能有问题
ServicesImpl_F service=new ServicesImpl_F();
这个service应该由spring注入
但是你是自己创建的对象,这样的话可能service中的一些属性没有被注入,比如说service中的dao,所以引起了空指针

------解决方案--------------------
探讨
引用:
如果你用的spring的话我感觉这里可能有问题
ServicesImpl_F service=new ServicesImpl_F();
这个service应该由spring注入
但是你是自己创建的对象,这样的话可能service中的一些属性没有被注入,比如说service中的dao,所以引起了空指针


刚开始学习,Spring的注入机制不是太明白,但我觉得你说的有道理,能否更具体点?
service在Action中可以直接引用,为什么在ActionForm中不可以?

------解决方案--------------------
对与dao对象是不能直接通过new实例话的,需要spring主入.
如果service实例话,要确保dao没有new ,通过application.getBean("")来获得bean的实例