SessionFactory给了我java.lang.NullPointerException

时间:2014-04-26 11:13:03

标签: java spring hibernate tomcat

我使用spring 3.2.8,hibernate 4.我有一个错误" HTTP状态500 - 请求处理失败;嵌套异常是java.lang.NullPointerException"

exception 
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
...

root cause 
java.lang.NullPointerException
    journeys.dao.CompanyDAO.f(CompanyDAO.java:41)
    journeys.controller.CompanyController.list(CompanyController.java:17)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...

的web.xml:

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <display-name>journeys</display-name>

 <servlet>
    <servlet-name>journeys</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>journeys</servlet-name>
        <url-pattern>/</url-pattern>
  </servlet-mapping>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/journeys-servlet.xml</param-value>
  </context-param>

  <listener>
       <listener-class>
          org.springframework.web.context.ContextLoaderListener
       </listener-class>
  </listener>

  <jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
  </jsp-config>
</web-app>

行程-servlet.xml中:

<beans xmlns="...">

    <context:component-scan base-package="journeys" />

    <bean id="viewResolver"
        ...
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://myserver:3306/name" />
        <property name="username" value="user" />
        <property name="password" value="password" />
    </bean>

    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="txManager222"
        class="journeys.dao.CompanyDAO">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="txManager" />

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven />
</beans>

类/ hibernate.cfg.xml中:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
      <!-- Mapping files -->
        <mapping class="journeys.entity.Company"/>
        ....
    </session-factory>
</hibernate-configuration>

CompanyDAO:

package journeys.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import journeys.utils.Pair;
import journeys.entity.Company;
import journeys.entity.Journey;
import journeys.entity.JourneyDeparture;
import journeys.entity.Order;

public class CompanyDAO extends AbstractDAO<Company> {
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    public CompanyDAO() {
        super(Company.class);
    }

    @SuppressWarnings("unchecked")
    public List<Company> f() {
        return (List<Company>)sessionFactory.getCurrentSession().createCriteria(Company.class).list();
    }
}

CompanyController:

package journeys.controller;

import journeys.dao.CompanyDAO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/company")
public class CompanyController {    
    @RequestMapping(value = {"", "/", "list"}, method = RequestMethod.GET)
    public ModelAndView list() {
        ModelAndView model = new ModelAndView("Company/list");
        CompanyDAO companydao = new CompanyDAO();
        model.addObject("companies", companydao.f());

        return model;
    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您的控制器代码不正确。 CompanyDAO companydao = new CompanyDAO();不正确。而不是创建CompanyDAO的实例将其注入。请参阅以下代码:

package journeys.controller;

import journeys.dao.CompanyDAO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/company")
public class CompanyController {
    @Autowired
    CompanyDAO companydao ;
    @RequestMapping(value = {"", "/", "list"}, method = RequestMethod.GET)
    public ModelAndView list() {
        ModelAndView model = new ModelAndView("Company/list");

        model.addObject("companies", companydao.f());

        return model;
    }
    public void setCompanydao(CompanyDAO companydao) {
        this.companydao = companydao;
    }


}

答案 1 :(得分:1)

调试它,并拆分一些调用。例如,你有

return (List<Company>)sessionFactory.getCurrentSession().createCriteria(Company.class).list();

创建条件,然后获取会话,然后获取列表,或其他任何内容。您可以在每个点测试null,或者您可以在此时在调试器中单步执行它,并找出哪个结果为null。像这样的长调用列表的一个问题不仅是如果你得到一个null就会崩溃,但是当你这么做时不知道哪个调用引起了问题。