SSMフレームワーク—java.lang.Null PointerException、空ポインタ異常処理

18974 ワード

Servlet.service() for servlet [GoodShop] in context with path [/goshop] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException
SSMプロジェクトをテストする時、ずっと空のポインタの異常を報告して、明らかに配置ファイルとコードはすべて間違いがなくて、どうしてこのようにしますか?
  30, 2018 1:17:53   org.apache.catalina.core.StandardWrapperValve invoke
 : Servlet.service() for servlet [GoodShop] in context with path [/goshop] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at com.xh.goshop.controller.GetRoleController.handleRequest(GetRoleController.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
    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:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

1回目の空のポインタ異常は、オブジェクトに注記@Autowiredを付けずにServiceレイヤに表示されます.
@Service("roleService")
public class RoleService implements IRoleService {

    @Autowired // , Bean
    private RoleMapper roleMapper;

    @Override
    public Role getRoleById(Long id) {
        // TODO Auto-generated method stub
        return roleMapper.getRole(id);
    }

}

2回目の空ポインタ異常はController層に現れた.modelメソッドでSpring Iocコンテナが初期化されていないため、beanインスタンスが取得されず、空ポインタ異常が報告されたためだ.
// Spring IoC Junit , 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring-mybatis.xml")
@Controller// URL
public class GetRoleController {

    //@Resource 
    //private RoleService roleService = null;

    @RequestMapping("/hello")// 
    public ModelAndView model(HttpServletRequest request, HttpServletResponse response) throws Exception {

//      ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring-mybatis.xml"); 
//      IRoleService roleService = ctx.getBean("roleService",RoleService.class);
        ModelAndView mv = new ModelAndView();
        Long id = 1L;
        System.out.println(" roleService ");
        System.out.println(roleService);
        Role role = roleService.getRoleById(id);
        mv.addObject("userinfo", role.getRoleName());
        mv.setViewName("/hello.jsp");       
        return mv;
    }

//  @Test
//  public void test() {
//      System.out.println(" roleServices ");
//      System.out.println(roleService);
//      Long id = 1L;
//      Role role = roleService.getRoleById(id);
//      System.out.println(role.getNote());
//  }

}

上のコードのTestクラスを独立してSpring IoCコンテナの初期化方式を比較すると、以下のようになります.
// Spring IoC Junit , 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring-mybatis.xml")
public class GetRoleController {

    @Resource 
    private RoleService roleService = null;

    @Test
    public void test() {
        System.out.println(" roleServices ");
        System.out.println(roleService);
        Long id = 1L;
        Role role = roleService.getRoleById(id);
        System.out.println(role.getNote());
    }

}

まとめ:空のポインタ異常処理を行い、注記@Autowiredまたは@Resourceを追加するかどうかを確認し、テスト用のSpring IoCコンテナの初期化方式を混同しないでください.