代码之家  ›  专栏  ›  技术社区  ›  skip

抽象向导窗体控制器:java.lang.NullPointerException异常在processFinish(…)

  •  0
  • skip  · 技术社区  · 14 年前

    有人能帮我理解为什么我在RegisterService出现这个错误吗ceProviderController.java 何时调用processFinish(…)方法?

    java.lang.NullPointerException
     at com.inception.web.RegisterServiceProviderController.processFinish(RegisterServiceProviderController.java:66)
     at org.springframework.web.servlet.mvc.AbstractWizardFormController.validatePagesAndFinish(AbstractWizardFormController.java:642)
     at org.springframework.web.servlet.mvc.AbstractWizardFormController.processFormSubmission(AbstractWizardFormController.java:492)
     at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:265)
     at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
     at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476)
     at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:441)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
     at java.lang.Thread.run(Thread.java:619)
    

    以下是运行此简单应用程序所需的所有文件: 注册服务ceProviderController.java

    package com.inception.web;
    
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.orm.hibernate3.HibernateTemplate;
    import org.springframework.validation.BindException;
    import org.springframework.validation.Errors;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.AbstractWizardFormController;
    
    import com.inception.bean.ExtendedServiceProvider;
    import com.inception.domain.BusinessLocation;
    import com.inception.domain.ServiceProvider;
    import com.inception.domain.State;
    import com.inception.service.ServiceProviderService;
    
    public class RegisterServiceProviderController extends AbstractWizardFormController{
    
     protected Object formBackingObject(HttpServletRequest request) throws Exception {
      ExtendedServiceProvider extendedServiceProvider = (ExtendedServiceProvider) super.formBackingObject(request);
    
      ServiceProvider serviceProvider = new ServiceProvider();
    
      Set<BusinessLocation> businessLocations = new HashSet<BusinessLocation>();
      BusinessLocation businessLocation = new BusinessLocation();
      businessLocation.setState(new State());
      businessLocations.add(businessLocation);
      serviceProvider.setBusinessLocations(businessLocations);
    
      extendedServiceProvider.setServiceProvider(serviceProvider);  
    
      return extendedServiceProvider;
     }
     protected Map referenceData(HttpServletRequest request, Object command, Errors errors, int page) throws Exception {
      ExtendedServiceProvider extendedServiceProvider = (ExtendedServiceProvider) command;
      Map refData = new HashMap();
      refData.put("currentLocationNumber", extendedServiceProvider.getServiceProvider().getBusinessLocations().size());
      refData.put("locationIndex", extendedServiceProvider.getServiceProvider().getBusinessLocations().size() - 1);
      refData.put("stateData", hibernateTemplate.find("from StateData"));
      return refData;
     }
    
     protected void postProcessPage(HttpServletRequest request, Object command, Errors errors, int page) throws Exception {
      ExtendedServiceProvider extendedServiceProvider = (ExtendedServiceProvider) command;
      if(page == 1 && request.getParameter("_target1") != null) {
       BusinessLocation businessLocation = new BusinessLocation();
       businessLocation.setState(new State());
       extendedServiceProvider.getServiceProvider().getBusinessLocations().add(businessLocation);   
      }
     }
    
     protected ModelAndView processFinish(HttpServletRequest request,
       HttpServletResponse response, Object command, BindException errors)
       throws Exception {
    
      ExtendedServiceProvider extendedServiceProvider = (ExtendedServiceProvider) command;
    
      //the last business location is always blank... remove it
      extendedServiceProvider.getServiceProvider().getBusinessLocations().remove(extendedServiceProvider.getServiceProvider().getBusinessLocations().size() - 1);
      serviceProviderService.addServiceProvider(extendedServiceProvider.getServiceProvider());
    
      return new ModelAndView(getSuccussView(), "serviceProvider", extendedServiceProvider.getServiceProvider());
     } 
    
     private String getSuccussView() {
      return getPages()[getPages().length - 1];
     }
    
     private ServiceProviderService serviceProviderService;
     public void setServiceProviderService(ServiceProviderService serviceProviderDAO) {
      this.serviceProviderService = serviceProviderService;
     }
    
     private HibernateTemplate hibernateTemplate;
     public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
      this.hibernateTemplate = hibernateTemplate;
     }
    
    }
    

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>springmvc</display-name>
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
        </servlet>
        <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
     </context-param>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.htm</url-pattern>
        </servlet-mapping>
    
     <servlet>
      <servlet-name>dwr</servlet-name>
      <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
      <init-param>
       <param-name>debug</param-name>
       <param-value>true</param-value>
      </init-param>
     </servlet> 
     <servlet-mapping>
      <servlet-name>dwr</servlet-name>
      <url-pattern>/dwr/*</url-pattern>
     </servlet-mapping>
    
     <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
    </web-app>
    

    调度员-servlet.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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.directwebremoting.org/schema/spring-dwr
          http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">
    
     <!-- DWR configuration -->
     <dwr:configuration >
      <dwr:convert type="bean" class="com.inception.domain.User" />
     </dwr:configuration>
    
     <!-- Resource bundle -->
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="messages"/>
        </bean>
    
        <!-- Data source -->
        <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/springmvc"/>
            <property name="username" value="root"/>
            <property name="password" value="password"/>
        </bean>
    
        <!-- Session factory -->
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
    
      <property name="annotatedClasses">
       <list>
        <value>com.inception.domain.ServiceProvider</value>
        <value>com.inception.domain.BusinessLocation</value>
        <value>com.inception.domain.State</value>
        <value>com.inception.domain.StateData</value> 
       </list>
      </property>
    
      <property name="hibernateProperties">
       <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.hbm2ddl.auto">create</prop>
        <prop key="hibernate.show_sql">true</prop>
       </props>
      </property>
    
     </bean>
    
     <!-- View resolver -->
     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
     </bean>
    
     <!-- Mapping handler -->
     <bean id="publicUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      <property name="mappings">
       <value>
        /register.htm=registerServiceProviderController
        /home.htm=homePageController
       </value>  
      </property>
      <property name="interceptors">
       <bean class="com.inception.interceptor.LoggingInterceptor" />
      </property>
     </bean>
    
     <!-- Controllers-->
    
     <bean id="registerServiceProviderController" class="com.inception.web.RegisterServiceProviderController">
      <property name="commandClass" value="com.inception.bean.ExtendedServiceProvider" />
      <property name="commandName" value="extendedServiceProvider" />
      <property name="sessionForm" value="true" />
      <property name="serviceProviderService" ref="serviceProviderService" />
      <property name="hibernateTemplate" ref="hibernateTemplate" />
      <property name="pages">
       <list>
        <value>numberOfLocationForm</value>
        <value>locationDetailForm</value>
        <value>registrationConfirmationForm</value>
        <value>redirect:home.htm</value>
       </list>
      </property>
     </bean>
    
     <bean id="homePageController" class="com.inception.web.HomePageController" />
    
     <!-- Services -->
     <bean id="serviceProviderService" class="com.inception.service.ServiceProviderServiceImpl">
      <property name="serviceProviderDAO" ref="serviceProviderDAO" />
     </bean>
    
     <!-- DAOs -->
     <bean id="serviceProviderDAO" class="com.inception.dao.ServiceProviderDAOImpl">
      <property name="hibernateTemplate" ref="hibernateTemplate" />
     </bean>
    
     <!-- Templates -->
     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
      <property name="sessionFactory" ref="sessionFactory" />
     </bean> 
    </beans>
    

    package com.inception.bean;
    
    import java.io.Serializable;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    import com.inception.domain.ServiceProvider;
    
    public class ExtendedServiceProvider implements Serializable{
    
     private Long locationCount;
     private ServiceProvider serviceProvider; 
    
     public Long getLocationCount() {
      return locationCount;
     }
     public void setLocationCount(Long locationCount) {
      this.locationCount = locationCount;
     }
     public ServiceProvider getServiceProvider() {
      return serviceProvider;
     }
     public void setServiceProvider(ServiceProvider serviceProvider) {
      this.serviceProvider = serviceProvider;
     }
    }
    

    package com.inception.domain;
    
    import java.io.Serializable;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    
    @Entity
    public class BusinessLocation implements Serializable{
    
     private Long id;
     private String address;
     private String city;
     private State state;
     private String pincode;
     private ServiceProvider serviceProvider;
    
     public BusinessLocation() {  
     }
    
     @Id
     @GeneratedValue
     public Long getId() {
      return id;
     }
     public void setId(Long id) {
      this.id = id;
     }
     public String getAddress() {
      return address;
     }
     public void setAddress(String address) {
      this.address = address;
     }
     public String getCity() {
      return city;
     }
     public void setCity(String city) {
      this.city = city;
     }
    
     @ManyToOne
     @JoinColumn(name="state_id")
     public State getState() {
      return state;
     }
     public void setState(State state) {
      this.state = state;
     } 
     public void setPincode(String pincode) {
      this.pincode = pincode;
     }
    
     public String getPincode() {
      return pincode;
     }
    
     @ManyToOne
     @JoinColumn(name="serviceProvider_id")
     public ServiceProvider getServiceProvider() {
      return serviceProvider;
     }
     public void setServiceProvider(ServiceProvider serviceProvider) {
      this.serviceProvider = serviceProvider;
     }
    }
    

    ServiceProvider.java

    package com.inception.domain;
    
    import java.io.Serializable;
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
    
    @Entity
    public class ServiceProvider implements Serializable{
    
     private Long id; 
     private Set<BusinessLocation> businessLocations = new HashSet<BusinessLocation>();
    
     @Id
     @GeneratedValue
     public Long getId() {
      return id;
     }
     public void setId(Long id) {
      this.id = id;
     }
    
     @OneToMany(mappedBy="serviceProvider", targetEntity=BusinessLocation.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)
     public Set<BusinessLocation> getBusinessLocations() {
      return businessLocations;
     }
    
     public void setBusinessLocations(Set<BusinessLocation> businessLocations) {
      this.businessLocations = businessLocations;
     }
    
    }
    
    **State.java**
    package com.inception.domain;
    
    import java.io.Serializable;
    import java.util.List;
    
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
    
    @Entity
    public class State implements Serializable {
    
     private Long id;
     private String name;
     private List<BusinessLocation> businessLocations;
    
     @Id
     @GeneratedValue 
     public Long getId() {
      return id;
     }
     public void setId(Long id) {
      this.id = id;
     }
     public void setName(String name) {
      this.name = name;
     }
    
     public String getName() {
      return name;
     }
    
    
     @OneToMany(mappedBy="state", targetEntity=BusinessLocation.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)
     public List<BusinessLocation> getBusinessLocations() {
      return businessLocations;
     }
     public void setBusinessLocations(List<BusinessLocation> businessLocations) {
      this.businessLocations = businessLocations;
     } 
    
    }
    

    状态数据.java

    package com.inception.domain;
    
    import java.io.Serializable;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity
    public class StateData implements Serializable{ 
    
     private Long id;
     private String name;
    
     @Id
     @GeneratedValue
     public Long getId() {
      return id;
     }
     public void setId(Long id) {
      this.id = id;
     }
     public String getName() {
      return name;
     }
     public void setName(String name) {
      this.name = name;
     }
    }
    

    package com.inception.dao;
    
    import java.util.List;
    
    import com.inception.domain.BusinessLocation;
    import com.inception.domain.ServiceProvider;
    
    public interface ServiceProviderDAO {
    
     public abstract void save(ServiceProvider serviceProvider);
     public abstract List<ServiceProvider> list();
     public abstract void save(BusinessLocation businessLocation);
    
    }
    

    硒ServiceProviderDaoImpl.java文件

    package com.inception.dao;
    
    import java.util.List;
    
    import org.springframework.orm.hibernate3.HibernateTemplate;
    
    import com.inception.domain.BusinessLocation;
    import com.inception.domain.ServiceProvider;
    
    public class ServiceProviderDAOImpl implements ServiceProviderDAO{
    
     private HibernateTemplate hibernateTemplate;
     public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
      this.hibernateTemplate = hibernateTemplate;
     }
    
     public List<ServiceProvider> list() {  
      return hibernateTemplate.find("from ServiceProvider");
     }
    
     public void save(ServiceProvider serviceProvider) {
      hibernateTemplate.save(serviceProvider);  
     }
    
     public void save(BusinessLocation businessLocation) {
      hibernateTemplate.save(businessLocation);  
     }
    
    }
    

    硒服务提供商服务.java

    package com.inception.service;
    
    import java.util.List;
    
    import com.inception.domain.ServiceProvider;
    
    public interface ServiceProviderService {
    
     public abstract void addServiceProvider(ServiceProvider serviceProvider);
     public abstract List<ServiceProvider> listAllServiceProviders();
    
    }
    

    package com.inception.service;
    
    import java.util.List;
    
    import org.springframework.transaction.annotation.Transactional;
    
    import com.inception.dao.ServiceProviderDAO;
    import com.inception.domain.BusinessLocation;
    import com.inception.domain.ServiceProvider;
    
    @Transactional
    public class ServiceProviderServiceImpl implements ServiceProviderService{
    
     private ServiceProviderDAO serviceProviderDAO;
     public void setServiceProviderDAO(ServiceProviderDAO serviceProviderDAO) {
      this.serviceProviderDAO = serviceProviderDAO;
     }
    
     public void addServiceProvider(ServiceProvider serviceProvider) {
      serviceProviderDAO.save(serviceProvider);
    
      for(BusinessLocation businessLocation: serviceProvider.getBusinessLocations()) {
       businessLocation.setServiceProvider(serviceProvider);
       serviceProviderDAO.save(businessLocation);
      }
    
     }
    
     public List<ServiceProvider> listAllServiceProviders() {
      return serviceProviderDAO.list();
     }
    
    }
    

    numberOfLocationForm.jsp

    <%@ page contentType="text/html" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    
    
    <form:form commandName="extendedServiceProvider" method="POST" action="register.htm">
     <input type="hidden" name="page" value="0" /><br>
     Number of locations: <form:input path="locationCount"/><br>
     <input type="submit" name="_target1" value="Next"> 
    </form:form>
    

    locationDetailForm.jsp文件

    <%@ page contentType="text/html" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    
    currentLocationNumber: ${currentLocationNumber}<br>
    locationIndex: ${locationIndex}<br><br>
    
    StateData coming from database: <c:forEach items="${stateData}" var="state" varStatus="status">
     <c:out value="${state.id}" /> - <c:out value="${state.name}" />
     <c:if test="${not status.last}">
      ,
     </c:if>
    </c:forEach><br><br>
    
    <form:form commandName="extendedServiceProvider" method="POST" action="register.htm">
     <tr>
      <td>locationCount: <c:out value="${extendedServiceProvider.locationCount}" /></td>
     </tr><br></br>
     <tr>
      <td>Address: </td>
      <td><form:input path="serviceProvider.businessLocations[${locationIndex}].address"/></td>
     </tr><br></br>
     <tr>
      <td>State: </td>
      <td>
       <form:select path="serviceProvider.businessLocations[${locationIndex}].state.id">
        <form:option value="0" label="--Have your pick--" />
        <form:options items="${stateData}" itemValue="id" itemLabel="name" />
       </form:select>
      </td>
     </tr><br></br>
        <input type="submit" name="_target0" value="Back">&nbsp;
        <input type="submit" name="_target1" value="Add Business Location">&nbsp;
        <input type="submit" name="_target2" value="Next">
    </form:form>
    

    <%@ page contentType="text/html" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    
    currentLocationNumber: ${currentLocationNumber}<br>
    locationIndex: ${locationIndex}<br><br>
    
    Registration confirmation:<hr />
    
    <form:form commandName="extendedServiceProvider" method="POST" action="register.htm">
     <tr>
      <td>locationCount: </td>
      <td><c:out value="${extendedServiceProvider.locationCount}" /></td>
     </tr><br>
     <tr>
      <td>
       <c:forEach items="${extendedServiceProvider.serviceProvider.businessLocations}" var="businessLocation">
        Address: <c:out value="${businessLocation.address}" /><br>
        State: <c:out value="${businessLocation.state.id}" /><br>
       </c:forEach>
      </td>
     </tr>
        <p>Click "Finish" to add service provider.</p>
        <input type="submit" name="_target1" value="Back">&nbsp;<input type="submit" name="_finish" value="Finish">
    </form:form><br><br>
    

    主页.jsp

    Congratulations, you've been redirected to our home page :)
    

    用于在上预填充select标记的sql代码locationDetailForm.jsp文件对于mysql

    INSERT INTO `statedata` (`id`,`name`) VALUES
     (1,'Alaska'),
     (2,'California'),
     (3,'Delaware'),
     (4,'Florida');
    

    谢谢

    1 回复  |  直到 14 年前
        1
  •  0
  •   skip    14 年前

    服务提供商DAO “作为参数变量(行 属于 RegisterServiceProviderController )当我应该 ". 我想知道你在哪里 服务提供商DAO

    以便 空指针异常 事情已经解决了。

    谢谢