1

I am using inside of my application's controller autowired repository that works fine. But I also want to use my repo inside of another class. But when I invoke any method from the second class I get null pointer exception, looks like Autowiring is not working inside of this class. What am I doing wrong? Is it impossible to Autowire the same repo more than once? Or maybe I should implement Service and call my repo via Service?

Error:

2017-05-15 16:27:52.193 ERROR 6624 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.logic.RecommendationEngine.generatedRecommendation(RecommendationEngine.java:116) ~[classes/:na]
    at com.controllers.HomeController.showRecommendation(HomeController.java:46) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.11.jar:8.5.11]

Repo interface:

public interface FundRepository extends CrudRepository<Fund, Long>{}

Controller with repo:

@RestController
public class RestServiceController {

    @Autowired
    private FundRepository fundRepo;

    //Some not related methods
}

Second class with the same repo:

public class RecommendationEngine {

    @Autowired
    private FundRepository fundRepo;

    //Method that invokes method from fundRepo but causes null pointer because fundRepo is null
}
Michael Dz
  • 3,655
  • 8
  • 40
  • 74

3 Answers3

1

Q : But when I invoke any method from the second class I get null pointer exception, looks like Autowiring is not working inside of this class. What am I doing wrong?

A : It's normal, as your RecommendationEngine class is not a spring bean ==> not handled by the spring context. So to make it works you should configure it as a bean ( using annotation or xml config ).

Q : Is it impossible to Autowire the same repo more than once?

A : You can call your spring beans as many as you want.

Abdelghani Roussi
  • 2,707
  • 2
  • 21
  • 39
1

Initialize your RecommendationEngine class object using @Autowired

@Autowired
RecommendationEngine engine;

And your RecommendationEngine class should look like so:

@Component
public class RecommendationEngine {

    @Autowired
    private FundRepository fundRepo;

    //Method that invokes method from fundRepo but causes null pointer because fundRepo is null
}
Afridi
  • 6,753
  • 2
  • 18
  • 27
-1

Try to annotate your RecommendationEngine with @Service, so the Spring Container can pick it up as a Spring Bean (managed Object)

@Service
public class RecommendationEngine {

    @Autowired
    private FundRepository fundRepo;
}

Some other class u can use it with:

@Controller
public class HomeController {

    @Autowired
    private RecommendationEngine recommendationEngine;


    // use it

}

Alternatively, you can use Project Lombok: https://projectlombok.org/ to reduce the Boilerplate Code for your dependencies in your class.

Example with Lombok:

@Controller
@RequiredArgsConstructor
public class HomeController {

    private final RecommendationEngine recommendationEngine;
    private final SomeOtherDependeny dependency;


    // use it

}

No need for manually @Autowired!

Dachstein
  • 3,994
  • 6
  • 34
  • 61