日期:2014-05-17  浏览次数:20776 次

Exception in thread "main" java.lang.NullPointerException
搞了一天一宿了,求高手求救,我只是用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)

------解决方案--------------------
很明显啊。你里面有个值是空的。根本就没取到。 可能是session问题。
你打印或者设置断点看什么地方抛出的异常就好了。easy
------解决方案--------------------
Java code

Session session = getSession();
        Transaction tx = session.beginTransaction();
        session.save(user)    
            tx.commit();
        session.close();

------解决方案--------------------
空值异常,你确定session里面有东西吗
------解决方案--------------------
很简单
就是你没有注入sessionFactory
配置一下就ok了
------解决方案--------------------
这是我的配置文件,你看一下吧,
XML code

<?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.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&amp;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=&