【问题标题】:@Controller throws NullPointerException when executes Service Object@Controller 在执行服务对象时抛出 NullPointerException
【发布时间】:2011-06-25 00:09:24
【问题描述】:

当我的@Controller 中的服务对象调用它的方法时,我得到了 NPE。任何人都可以帮助我。我是一个新手,这真的让我很困惑。

这是我的 applicatoncontext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.test.paymentinterface.ui" />
<bean id="mainService" class="com.test.paymentinterface.service.MainService" />

<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

我的控制器类被关注

package com.thehutgroup.paymentinterface.ui;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.thehutgroup.paymentinterface.service.MainService;

@Controller
public class MainController {

private MainService mainService;

@Autowired
public void setMainService(MainService mainService) {
    this.mainService = mainService;
}

@RequestMapping(value = "/", method = RequestMethod.GET)
public ResponseEntity<String> healthcheck() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "text/plain");
    return new ResponseEntity<String>(mainService.demoServiceResponse(), headers, HttpStatus.OK);
  }
}

服务类如下

package com.thehutgroup.paymentinterface.service;
public class MainService {
public String demoServiceResponse(){
    return "Main Service has returned demo Service Response";
}
}

我也尝试使用@Service 和@Component 标签来注释服务类,但每次调用时都会抛出 NullPointerException

mainService.demoServiceResponse()

带有healthCheck webservice的返回语句,即

return new ResponseEntity<String>(mainService.demoServiceResponse(), headers, HttpStatus.OK);

跟踪堆栈跟踪。

java.lang.NullPointerException
    at com.thehutgroup.paymentinterface.ui.MainController.healthcheck(MainController.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    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:127)
    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:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)

请帮助我,我在网络上找不到任何解决此问题的方法。

【问题讨论】:

    标签: model-view-controller spring controller nullpointerexception


    【解决方案1】:
    <context:component-scan base-package="com.test.paymentinterface.ui" />
    

    而且包裹是

    package com.thehutgroup.paymentinterface.ui;
    package com.thehutgroup.paymentinterface.service;
    

    所以你的类不是 bean 自动装配的。

    另外请补充

     <context:annotation-config/> 
    

    在您的 XML 配置文件中

    【讨论】:

      猜你喜欢
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多