使用spring时,HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException

坚默不语hj 发布于 2017/05/28 22:54
阅读 2K+
收藏 0

ervlet.service() for servlet [spring] in context with path [/home_furniture] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at com.neu.action.FurnitureAction.getAllFurnitureList(FurnitureAction.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)


Action.java

package com.neu.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neu.model.Furniture;
import com.neu.service.FurnitureService;

@Controller
public class FurnitureAction {
    @Autowired
    private FurnitureService furnitureservice;

    public FurnitureService getFurnitureservice() {
        return furnitureservice;
    }
    public void setFurnitureservice(FurnitureService furnitureservice) {
        this.furnitureservice = furnitureservice;
    }
    
    @RequestMapping("/getAllFurnitureList.do")
    public String getAllFurnitureList(Map<String,List<Furniture>> m){
        List<Furniture> furniture=furnitureservice.getAllFurniture();
        
        for(Furniture f:furniture){
            System.out.println("测试"+f.getFurniture_name());
        }
        m.put("furniture", furniture);
        return "login";
    }
    
    
    
    
}

 

 


service.java

package com.neu.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.neu.mapper.FurnitureMapper;
import com.neu.model.Furniture;


@Service
public class FurnitureService {
    @Resource
    private FurnitureMapper furnituremapper;
    public List<Furniture> getAllFurniture() {
        // TODO Auto-generated method stub
        List<Furniture> furniture=furnituremapper.getAllFurniture();
        return furniture;
    }

}

Mapper.java

package com.neu.mapper;

import java.util.List;

import com.neu.model.Furniture;

public interface FurnitureMapper {

    public List<Furniture> getAllFurniture();

}

Mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper  
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"   
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.neu.mapper.FurnitureMapper">
    <select id="getAllFurniture"  resultType="Furniture">
         select id fid,furniture_name fname,furniture_price fprice,furniture_num fnum,
         furniture_describe fdescribe,furniture_style fstyle,furniture_picture fpicture from furniture
    </select>
    
</mapper>

 

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>Spring Web MVC Hello World Application</display-name>

    <!-- Spring MVC配置 -->
    <!-- ====================================== -->
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置文件  -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:config/spring-*.xml</param-value>
    </init-param>
    <!--
    1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。
    
    2)它的值必须是一个整数,表示servlet应该被载入的顺序
    2)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet;
    
    3)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。
    4)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。
    5)当值相同时,容器就会自己选择顺序来加载。
    所以,<load-on-startup>x</load-on-startup>,中x的取值1,2,3,4,5代表的是优先级,而非启动延迟时间。
    
      -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 把所有的请求都交给了SpringMVC,并配置使用文件的位置  -->
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
  <!-- 解决乱码  -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 欢迎页面  -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>


Spring_servlet.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>Spring Web MVC Hello World Application</display-name>

    <!-- Spring MVC配置 -->
    <!-- ====================================== -->
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置文件  -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:config/spring-*.xml</param-value>
    </init-param>
    <!--
    1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。
    
    2)它的值必须是一个整数,表示servlet应该被载入的顺序
    2)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet;
    
    3)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。
    4)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。
    5)当值相同时,容器就会自己选择顺序来加载。
    所以,<load-on-startup>x</load-on-startup>,中x的取值1,2,3,4,5代表的是优先级,而非启动延迟时间。
    
      -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 把所有的请求都交给了SpringMVC,并配置使用文件的位置  -->
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
  <!-- 解决乱码  -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 欢迎页面  -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>


Mybatis_config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
     <!-- 别名 -->
     <typeAliases>
         
         <typeAlias type="com.neu.model.User" alias="User" />
         <typeAlias type="com.neu.model.Furniture" alias="Furniture" />
     </typeAliases>
   
    
     <!-- 定义映射器 -->
     <mappers>
         <mapper resource="com/neu/mapper/UserMapper.xml" />
         <mapper resource="com/neu/mapper/FurnitureMapper.xml" />
         
     </mappers>
</configuration>

jsp部分截取

<!-- 这里的cates是在Action中存储到map对象中的,m.put(“furniture”,furniture),与第一个参数名字一致 -->
                <c:if test="${!empty furniture }">
                    <!-- 这个itmes表示要循环处理的集合,var是迭代变量,第一遍循环表示第一个元素,第n变循环表示第n元素
     varStatus表示循环状态,index属性表示第几个-->
                    <c:forEach items="${furniture}" var="f" varStatus="vs">
                        <c:if test="${vs.count<=6}">
                            <div class="new-items">
                                <div class="item1">
                                    <a href="products.html">
                                    <img src="${pageContext.request.contextPath}${f.fpicture}"
                                    alt="${f.fname}" /></a>
                                    <div class="item-info">
                                        <!-- <h4>
                                            <a href="products.jsp">Brown Furny Seater</a>
                                        </h4>
                                        <span>ID: SR5421</span> <a href="single.jsp">Buy Now</a> -->
                                    </div>
                                </div>

                            </div>
                        </c:if>
                    </c:forEach>
                </c:if>

      

加载中
0
安西都护府首席程序员
安西都护府首席程序员

你的FurnitureService 使用的@service注解,有在spring配置文件配置扫描吗?

看上面报的是空指针异常,应该是FurnitureService 为空吧?

0
阿信sxq
阿信sxq

纯粹是在那里乱整,报错信息里面说/home_furniture,但是你并没有配置这个路径啊

返回顶部
顶部