搞了一天一宿了,求高手求救,我只是用SSH做了一个简单的注册,注册值我给的是死值,就是固定的,还是会出现这个异常,请高手来指点,谢谢Exception in thread "main" java.lang.NullPointerException
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:142)
at dao.UserDAO.save(UserDAO.java:39)
at test.Test.main(Test.java:19)
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:142)
at dao.UserDAO.save(UserDAO.java:39)
at test.Test.main(Test.java:19)
你打印或者设置断点看什么地方抛出的异常就好了。easy
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import vo.User;public class UserDAO extends HibernateDaoSupport {
public void save(User user){
Session session = getSession();
session.clear();
session.save(user);
}
}
Session session = getSession();这句话这儿出异常了,不知道为什么
Session session = getSession();
Transaction tx = session.beginTransaction();
session.save(user)
tx.commit();
session.close();
package test;import vo.User;
import dao.UserDAO;public class Test {/**
* @param args
*/
public static void main(String[] args) {
UserDAO userDAO = new UserDAO();
User user = new User("1","1",1,"1","1");
userDAO.save(user);
System.out.println("!!!");
}}
<?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.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/users">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>vo/User.hbm.xml</value></list>
</property></bean></beans>
就是你没有注入sessionFactory
配置一下就ok了
<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.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- ========================= GENERAL DEFINITIONS ========================= -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="1000"/>
<property name="defaultAutoCommit" value="true"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
<property name="logAbandoned" value="true"/>
</bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource" ref="dataSource"/>
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <tx:advice id="txAdvice" transaction-manager="txManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="get*" read-only="true"/>
<tx:method name="load*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice> <!-- ensure that the above transactional advice runs for any execution
of an operation defined by the FooService interface com.dtb.web.impl-->
<aop:config>
<aop:pointcut id="webServiceOperation" expression="execution(* impl..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="webServiceOperation"/>
</aop:config></beans>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- 配置hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 依赖注入SessionFactory所需要的DataSource -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载所有的映射文件 -->
<property name="mappingResources">
<!-- 下面列出所有的持久化映射文件 -->
<list>
<!-- <value>com/dtb/web/model/news/News.hbm.xml</value> -->
</list>
</property>
<!-- 指定hibernate的属性 -->
<property name="hibernateProperties">
<props>
<!-- 指定hibernate使用的数据库方言 org.hibernate.dialect.MySQLDialect-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>vo/User.hbm.xml</value>
</list>
</property>
</bean>
this.getHibernateTemplate().getSessionFactory().getCurrentSession().clear();
this.getHibernateTemplate().getSessionFactory().getCurrentSession().clear(); 也不行