错误:
java.lang.NullPointerException
at com.fund.manager.impl.UserManagerImpl.login(UserManagerImpl.java:52)
at com.fund.web.actions.LoginAction.execute(LoginAction.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
……………………hibernate.cfg.xml
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/fund</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>

<mapping resource="com/fund/model/Party.hbm.xml"/>
<mapping resource="com/fund/model/Acl.hbm.xml"/>
<mapping resource="com/fund/model/OtherMapping.hbm.xml"/>applicationContext-manager.xml
    </bean> <!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="txProxyTemplate" abstract="true"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager"><ref bean="transactionManager"/></property>
        <property name="transactionAttributes">
            <props>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="edit*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="del*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="login">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
         <bean id="userManager" parent="txProxyTemplate">
        <property name="target">
            <bean class="com.fund.manager.impl.UserManagerImpl">
             <property name="sessionFactory" ref="sessionFactory"/>
            </bean>
        </property>
    </bean>
User.hbm.xml
    <class name="com.fund.model.User" table="t_User">
     <id name="id">
            <generator class="native"/>
        </id>
        <property name="account"/>
        <property name="password"/>
        <property name="expireTime"/>
        <many-to-one name="person" unique="true" lazy="false"/>
        <set name="roles" table="T_User_Roles">
         <key column="userid" />
         <many-to-many class="com.fund.model.Role" column="roleid" lazy="false" />
        </set>
    </class>java代码
ACTION类public String execute() throws Exception {
//注入
User user = userManager.login(getAccount(), getPassword());

UserManager u = new UserManagerImpl();
User user = u.login(getAccount(), getPassword());
//转向main.jsp
return "main";
}Manager类List list = getHibernateTemplate().find("from User where account = ?",username);问题:我一执行Manager类就报空指针异常,getHibernateTemplate()是空的不知道为什么,这个问题搞了我好几天了,非常着急,请各位帮我看看问题到底出再哪了?谢谢各位啊

解决方案 »

  1.   

    UserManager u = new UserManagerImpl(); 
    怎么是new出来的???
      

  2.   

    Action的开头这样写:
     
        UserManager userManager;加上get,set方法。下边不要new,直接用userManager;就可以了。意思是,容器中已经有userManager了,可以拿来用。
      

  3.   

    但是要为你的Action配置相应的UserManager
      

  4.   


    呵呵
    好象不是new出来的哦
    ssh就是配置起来会多一点另外hibernate里可以多对多关联
    但是不要太多哦
    会影响性能
      

  5.   

    User user = userManager.login(getAccount(), getPassword()); UserManager u = new UserManagerImpl(); 
    User user = u.login(getAccount(), getPassword()); 
    这个new出来就有问题的你这样new出来他就没有注入了;这样调用该方法他就会报getHibernateTemplate()是空了
      

  6.   

    各位对不起,上面代码是我忽略了,正确的Action应该是:User user = userManager.login(getAccount(), getPassword()); userManager已经注入进去了,谢谢,待解决