实体类:
public class Users implements java.io.Serializable {
private int id;
private String name;
private String pass;
   Get set省略
}
映射文件:(表名与字段名都和实体类中一样)
<hibernate-mapping>
    <class name="com.ssh.Users" table="Users" >
        <id name="id" type="java.lang.Integer">
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String"></property>
        <property name="pass" type="java.lang.String"></property>
    </class>
</hibernate-mapping>接口:
public interface UsersDao {
public Users get(int id);
}
接口实现类:
public class UsersDaoHiberImpl extends HibernateDaoSupport
                                implements UsersDao{
public Users get(int id)
{
          return (Users)super.getHibernateTemplate() .get(Users.class, id);
}

}Hibernate配置文件:
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;databasename=zf
</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.username">zf</property>
<property name="connection.password">zf</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/ssh/Users.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
   </bean> 
   <bean id="usersDao" class="com.ssh.UsersDaoHiberImpl">
   <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
</beans>Main方法中代码:
UsersDao ud=new UsersDaoHiberImpl();
Users u=(Users)ud.get(1);
System.out.println(u.getName());运行后:
Exception in thread "main" java.lang.NullPointerException
at com.ssh.UsersDaoHiberImpl.get(UsersDaoHiberImpl.java:20)
at test.T.main(T.java:30)
20行指的是:
return (Users)super.getHibernateTemplate().get(Users.class, id);

解决方案 »

  1.   

    你自己new了个对象出来,spring没办法帮你注入HibernateTemplete,
    所有getHibernateTemplate()为null,建议在dao配置到spring,通过
    spring容器获取dao
      

  2.   

    Users.class    这个类 好好看下,应该是这个 空的吧
      

  3.   

    我在IE中运行时没有问题,eclipse中单独运行Main()中代码就会空指针,
    不new对象那我要怎么测试Dao层方法是不是对的啊
      

  4.   

    ApplicationContext ctx = new ClassPathXmlContext("applicationContext.xml");
    UserDao userDao = (UserDao) ctx.getBean("dao在spring中配置的名字");
      

  5.   

    Main方法中代码:
    UsersDao ud=new UsersDaoHiberImpl(); 
    这个是你自己new出来的,不受spring控制,无法实现注入
    要用楼上的方法来使用
      

  6.   

    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class TestDao {
    Logger logger = Logger.getLogger(this.getClass());
    ApplicationContext ac = new ClassPathXmlApplicationContext(
    "applicationContext.xml");  @Before
    public void setUp() throws Exception {
    System.out.println("before");
    }  @After
    public void tearDown() throws Exception {
    System.out.println("after");
    }        @Test
    public void testUserDao()
    {
    UserDao udao=(VscheduleServiceImpl) ac.getBean("usersDao");
                    User u=udao.get(1);
                    //...
            }
    }
         
      

  7.   

    自己new出来的,不受spring控制,不能实现注入
      

  8.   

    spring注入不能自己new,要先获得IOC容器,然后从IOC容器里获得Bean不这么做获得不了你要的注入。