我使用spring 3.2.8,休眠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>


trips-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>


class / 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;
    }
}


我究竟做错了什么?

最佳答案

您的控制器代码不正确。 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;
    }


}

关于java - SessionFactory给我java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23310041/

10-09 08:00