1

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.

Here is my 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" />

My Controller class is followed

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

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(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)

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

1 Answer 1

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

and the package is

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

so your classes are not bean autowired.

In addition please add

 <context:annotation-config/> 

in your XML conf

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.