当前位置: 代码迷 >> J2EE >> hibernate java.lang.NullPointerException解决方案
  详细解决方案

hibernate java.lang.NullPointerException解决方案

热度:49   发布时间:2016-04-22 03:07:51.0
hibernate java.lang.NullPointerException
在做一个关于struts2+spring+hibernate3.x项目搭建环境测试过程中,写一个主函数测试hibernate的查询和存储,出现
java.lang.NullPointerException ,上网找了好多信息,也没有解决问题,贴出来,哪个好心人帮助看一下!

public class UsersDAOImpl extends BaseDAOImpl implements IUsersDAO {

  public Users findUserByNameAndPass(String name, String pass) {
System.out.println("程序执行到此...UserDAOImpl...findUserByName"+name+"..."+pass);

//String sql = "from Users s where s.username=? and s.userpassword=?";
List ul = getHibernateTemplate().find("from Users s where s.username="+name+"s.userpassword="+pass,new String[]{name,pass});

if(ul!=null && ul.size()>=1){
return (Users)ul.get(0);
}
return null;
  }
}
========================================================================

测试的主函数:
public static void main(String[] args) {
// TODO Auto-generated method stub
//
// UserServiceImpl usi = new UserServiceImpl();
// try {
// usi.loginValid("admin", "123");
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
UsersDAOImpl udi = new UsersDAOImpl();
// //udi.findUserByNameAndPass("admin","123");
// //udi.getAllUser();
//
Users user = new Users();
user.setUsername("gaochc");
udi.saveObj(user);
}
========================================================================
报出的错误异常:

Exception in thread "main" java.lang.NullPointerException
at net.gcc.pms.dao.impl.BaseDAOImpl.saveObj(BaseDAOImpl.java:27)
at net.gcc.pms.test.TestClass.main(TestClass.java:28)
========================================================================
<?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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

  <!-- 设置数据源对象 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@127.0.0.1:1521:oral</value>
</property>
<property name="username">
<value>gaochc</value>
</property>
<property name="password">
<value>19820415</value>
</property>

</bean>
  <!-- 配置sessionFactory,注意这里引入的是hibernate3而不是hibernate -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>net/gcc/pms/vo/Users.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
  相关解决方案