当前位置: 代码迷 >> Java Web开发 >> 空指针错误
  详细解决方案

空指针错误

热度:366   发布时间:2010-11-24 15:30:28.0
空指针错误
各位大虾好我Spring手工集成hibernate登录程序报空指针错误请帮忙解决下
applicationContext.xml代码:

<?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.apache.commons.dbcp.BasicDataSource" destroy-method="close">
          <property name="driverClassName" value="com.mysql.jdbc.Driver" />
          <property name="url" value="jdbc:mysql://localhost:3306/myweb" />
          <property name="username" value="root" />
          <property name="password" value="111111" />
    </bean>
   
    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
  <property name="configLocation" ref="dataSource" />
  <property name="mappingResources">
    <list>
      <value>classpath:User.hbm.xml</value>
    </list>
  </property>
  <property name="hibernateProperties">
      <props>
          <prop key="hibernate.dialect">MySQLDialect</prop>
      </props>
  </property>
</bean>
<aop:config >
        <aop:pointcut id="allservicemanager" expression="execution(* com.ssh.service.*.*(..))"/>
        <aop:advisor pointcut-ref="allservicemanager" advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>


<bean id="txManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
  <property name="sessionFactory">
      <ref local="sessionFactory"/>
  </property>
</bean>
    <bean id="hibernteTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
  <property name="sessionFactory" ref="sessionFactory"/>
</bean>
      <bean id="loginDao" class="com.ssh.dao.LoginDao">
    <property name="hibernteTemplate" ref="hibernteTemplate" />
</bean>
<bean id="loginService" class="com.ssh.service">
    <property name="loginDao" ref="loginDao" />
    <property name="txManager" ref="txManager" />
</bean>
</beans>

LoginService.java代码
package com.ssh.service;

import java.util.List;

import com.ssh.dao.LoginDao;
import com.ssh.pojo.User;

public class LoginService {
   
    private LoginDao loginDao;
   
    public void login(User user){
    List list = loginDao.getUser();
    User user1 = (User)list.get(0);
    if(user.getUsername().equals(user1.getUsername()) && user.getPassword().equals(user1.getPassword())){
        
        System.out.println("success" + "username=" +user1.getUsername());
        System.out.println("success" + "username=" +user1.getPassword());
    }else{
        System.out.println("error!!");
    }
    }
    public LoginDao getLoginDao() {
        return loginDao;
    }

    public void setLoginDao(LoginDao loginDao) {
        this.loginDao = loginDao;
    }
}
User.java 代码
package com.ssh.pojo;

public class User {
   
    private int id;
   
    private String username;
   
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
User.hbm.xml代码:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
    <class name="com.ssh.pojo.User" table="user">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="username"/>
        <property name="password" />
    </class>
</hibernate-mapping>
LoginDao.java代码
package com.ssh.dao;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;



public class LoginDao {
   
    private HibernateTemplate hibernateTemplate;
    public List getUser(){
        
        List list = hibernateTemplate.find("from User");
        System.out.print(list);
        return list;
    }
    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }
}
其中的  List list = hibernateTemplate.find("from User");这一句报出空指针错误


搜索更多相关主题的帖子: 指针  

----------------解决方案--------------------------------------------------------
你的spring 文件里面的sessionfactory配置肯定有问题.得不到数据库的连接
测试下是否连接了数据库!

----------------解决方案--------------------------------------------------------
最好是让他将启动日志、异常堆栈都贴出来,这样判断才能准确
----------------解决方案--------------------------------------------------------
  相关解决方案