当前位置: 代码迷 >> Java Web开发 >> HibernateTemplate().find()方法出现java.lang.NullPointerException错误
  详细解决方案

HibernateTemplate().find()方法出现java.lang.NullPointerException错误

热度:805   发布时间:2016-04-17 00:22:44.0
HibernateTemplate().find()方法出现java.lang.NullPointerException异常
不管数据库里有没有值,都报空指针

以下是代码:
/**
* 跳转到薪酬标准登记
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/ public ActionForward toSalaryCheckIn(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {

String id=salaryStandardCheckInBiz.getMaxStandarId();//获得最大薪酬编号,如果没有返回默认
salaryStandard.setStandardId(id);//薪酬编号

salaryStandard.setRegistTime(new Date());//登记时间

request.setAttribute("salaryStandard", salaryStandard);

List confList=salaryStandardCheckInBiz.getItemName("薪酬设置");//根据属性的种类查询出薪酬项目

request.setAttribute("confList", confList);


return mapping.findForward("salaryStandardCheckIn");//跳转到薪酬标准登记页面
}

daoImpl:
/**
* 查询薪酬标准信息的最大薪酬标准单编号
*/
public String getMaxStandarId() {
String hql = "select MAX(s.standardId) from SalaryStandard s";
String id = null;
try{
List list = super.getHibernateTemplate().find(hql);
Iterator itr = list.iterator();
while(itr.hasNext()) {
SalaryStandard salaryStandard = (SalaryStandard) list.get(0);
if(salaryStandard.getStandardId()==null){
id="1000000001";
}else{
id=salaryStandard.getStandardId();//获得薪酬编号
}
}
}catch(RuntimeException e){
e.printStackTrace();
}

return id;
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<!-- 事务管理器 -->
<bean id="myHibTxManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 实体bean -->
<bean id="salaryStandard" class="org.hr.entity.SalaryStandard"/>

<!-- dao -->
<bean id="humanFileDAO" class="org.hr.dao.impl.HumanFileDAOImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="salaryStandardCheckInDao" class="org.hr.dao.impl.SalaryStandardCheckInDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- biz -->
<bean id="humanFileBiz" class="org.hr.biz.impl.HumanFileBizImpl"/>
<bean id="salaryStandardCheckInBiz" class="org.hr.biz.impl.SalaryStandardCheckInBizImpl"/>
<!-- action -->
<bean name="/humanFile" class="org.hr.struts.action.HumanFileAction"/>
<bean name="/salaryStandardCheckIn" class="org.hr.struts.action.SalaryStandardCheckInAction"/>






  <!-- 事务通知 -->
<tx:advice id="txadvice" transaction-manager="myHibTxManager">
<tx:attributes>
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
  相关解决方案