PDA

View Full Version : @Controller throws NullPointerException when executes Service Object's method



raza_amir@hotmail.com
Jun 24th, 2011, 07:27 PM
I am getting NPE when the service object in my @Controller calls its method. Can Any one help me please. I am a newbie and this is really stucking me.


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.Def aultAnnotationHandlerMapping" />

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


My Controller class is followed


package com.thehutgroup.paymentinterface.ui;

import org.springframework.beans.factory.annotation.Autow ired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMap ping;
import org.springframework.web.bind.annotation.RequestMet hod;

import com.thehutgroup.paymentinterface.service.MainServi ce;

@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);
}
}


and the service class is as followed


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

I've tried to annotate the service class with @Service and @Component tags too but every time it throws the NullPointerException when it calls



mainService.demoServiceResponse()

with the return statement of healthCheck webservice i.e.


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

the stack trace is followed.



java.lang.NullPointerException at com.thehutgroup.paymentinterface.ui.MainController .healthcheck(MainController.java:32) at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.web.bind.annotation.support.Ha ndlerMethodInvoker.invokeHandlerMethod(HandlerMeth odInvoker.java:176) at org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An notationMethodHandlerAdapter.java:426) at org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.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.p rocessRequest(FrameworkServlet.java:644) at org.springframework.web.servlet.FrameworkServlet.d oGet(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.in ternalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invo ke(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.invok e(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:857) at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:619)



Please help me in this, I couldn't find any fix for this problem on web.

mers
Jun 25th, 2011, 09:33 AM
In your Spring configuration file, try component scanning com.thehutgroup.paymentinterface.ui and specify <mvc:annotation-driven />.

HTH,
Emerson

raza_amir@hotmail.com
Jun 25th, 2011, 10:25 AM
Mers thank you very much for your reply.

I've followed your both instructions but the result is same. I am still not able to fix this issue. Just another thing that if i declare my service object as static then it works fine but non-static does't work at all. The problem is still there.

TerpInMD
Jun 25th, 2011, 12:33 PM
Might need to add <context:annotation-config /> also you should put an interface infront of that service or you will have to turn on class based proxying.