@Controller在执行服务对象时抛出NullPointerException

时间:2011-06-25 00:09:25

标签: model-view-controller spring controller nullpointerexception

当@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语句,即

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)

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

1 个答案:

答案 0 :(得分:3)

<context:component-scan base-package="com.test.paymentinterface.ui" />

并且包是

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

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

另外请添加

 <context:annotation-config/> 
您在XML conf中的