Spring MVC/Hibernate: Autowired service in controller is null












1















I'm trying to @Autowire a @Service in a @Controller. This results in a runtime NullPointerException (where I attempt to make use of the @Service). For some reason Spring isn't injecting the dependency and I'm yet to figure it out. It doesn't seem to be because of the two primary causes (manual instanstiation or lack of annotations).



ReviewController:



package group9.movie_reviews.controller;

import group9.movie_reviews.service.impl.ReviewService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;

@Controller
@RequestMapping("/review")
@SessionAttributes("review")
public class ReviewController {

@Resource
private ReviewService reviewService;

@RequestMapping(method = RequestMethod.GET)
public String wtf(ModelMap model) {
model.addAttribute("a", reviewService.getReviews()); // NullPointerException here
return "index";
}

}


ReviewService:



package group9.movie_reviews.service;

import group9.movie_reviews.model.ReviewEntity;

public interface ReviewService {

Iterable<ReviewEntity> getReviews();

void addReview(String movieName);

}


ReviewServiceImpl:



package group9.movie_reviews.service.impl;

import group9.movie_reviews.model.ReviewEntity;
import group9.movie_reviews.repository.ReviewRepository;
import group9.movie_reviews.service.ReviewService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ReviewServiceImpl implements ReviewService {

@Resource
private ReviewRepository reviewRepository;

@Override
public Iterable<ReviewEntity> getReviews() {
return reviewRepository.findAll();
}

@Override
public void addReview(String movieName) {
ReviewEntity r = new ReviewEntity();
r.setMovieName(movieName);
reviewRepository.save(r);
}

}


ReviewRepository:



package group9.movie_reviews.repository;

import group9.movie_reviews.model.ReviewEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ReviewRepository extends JpaRepository<ReviewEntity, Integer> {
ReviewEntity findByMovieName(String movieName);
}


Stacktrace/error:



Type Exception Report

Message Request processing failed; nested exception is java.lang.NullPointerException

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Root Cause

java.lang.NullPointerException
group9.movie_reviews.controller.ReviewController.wtf(ReviewController.java:21)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


Config Files:



WebConfig:



package group9.movie_reviews.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"group9.movie_reviews"})
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"group9.movie_reviews.repository"})
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}

}


WebInit:



package group9.movie_reviews.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

@Configuration
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?> getRootConfigClasses() {
return new Class{RootConfig.class};
}

@Override
protected Class<?> getServletConfigClasses() {
return new Class{WebConfig.class};
}

@Override
protected String getServletMappings() {
return new String{"/", "/review"};
}

}


Redeploy exception/stacktrace:



[2018-11-22 02:53:37,857] Artifact movie_reviews:war exploded: Artifact is being deployed, please wait...
22-Nov-2018 02:53:37.878 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose Closing WebApplicationContext for namespace 'dispatcher-servlet': startup date [ Thu Nov 22 02:35:08 GMT 2018]; parent: Root WebApplicationContext
22-Nov-2018 02:53:37.878 WARNING [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose Exception thrown from LifecycleProcessor on context close
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Nov 22 02:35:08 GMT 2018]; parent: Root WebApplicationContext
at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:416)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:997)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:956)
at org.springframework.web.servlet.FrameworkServlet.destroy(FrameworkServlet.java:829)
at org.apache.catalina.core.StandardWrapper.unload(StandardWrapper.java:1393)
at org.apache.catalina.core.StandardWrapper.stopInternal(StandardWrapper.java:1729)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5573)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:851)
at org.apache.catalina.startup.HostConfig.unmanageApp(HostConfig.java:1755)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.removeContext(MBeanFactory.java:792)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
at sun.reflect.GeneratedMethodAccessor238.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:835)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

22-Nov-2018 02:53:37.880 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose Closing Root WebApplicationContext: startup date [Thu Nov 22 02:35:08 GMT 2018]; root of context hierarchy
22-Nov-2018 02:53:40.095 INFO [RMI TCP Connection(83)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
22-Nov-2018 02:53:40.197 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization started
22-Nov-2018 02:53:40.305 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.prepareRefresh Refreshing Root WebApplicationContext: startup date [Thu Nov 22 02:53:40 GMT 2018]; root of context hierarchy
22-Nov-2018 02:53:40.400 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.loadBeanDefinitions Registering annotated classes: [class group9.movie_reviews.config.RootConfig]
22-Nov-2018 02:53:40.709 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization completed in 512 ms
22-Nov-2018 02:53:40.713 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'dispatcher': initialization started
22-Nov-2018 02:53:40.719 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.prepareRefresh Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Nov 22 02:53:40 GMT 2018]; parent: Root WebApplicationContext
22-Nov-2018 02:53:40.722 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.loadBeanDefinitions Registering annotated classes: [class group9.movie_reviews.config.WebConfig]
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[2018-11-22 02:53:41,443] Artifact movie_reviews:war exploded: Artifact is deployed successfully
[2018-11-22 02:53:41,443] Artifact movie_reviews:war exploded: Deploy took 3,586 milliseconds


Pom.xml:



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>group9</groupId>
<artifactId>movie_reviews</artifactId>
<version>1.0</version>
<packaging>war</packaging>

<name>movie_reviews</name>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-version>4.3.3.RELEASE</spring-version>
<servlet-version>3.1.0</servlet-version>
<jstl-version>1.2</jstl-version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>

</project>









share|improve this question




















  • 1





    Let's see a stacktrace.

    – Sotirios Delimanolis
    Nov 22 '18 at 2:18











  • @SotiriosDelimanolis It's a NullPointerException :/ added anyway

    – user3278983
    Nov 22 '18 at 2:28











  • Making sure you're reading it right. Do you have a CommonAnnotationBeanPostProcessor bean registered to process the @Resource annotation?

    – Sotirios Delimanolis
    Nov 22 '18 at 2:31











  • @SotiriosDelimanolis I don't know what that is so I guess not. I have tried this with @Autowired annotations also, but intelliJ complains less with @Resource annotations. Still get the same error with @Autowired

    – user3278983
    Nov 22 '18 at 2:33











  • could you also attach the spring.config that you are using, xml or class config? Also as mentioned above , resource is used to define endpoints and not just services

    – AntJavaDev
    Nov 22 '18 at 2:39


















1















I'm trying to @Autowire a @Service in a @Controller. This results in a runtime NullPointerException (where I attempt to make use of the @Service). For some reason Spring isn't injecting the dependency and I'm yet to figure it out. It doesn't seem to be because of the two primary causes (manual instanstiation or lack of annotations).



ReviewController:



package group9.movie_reviews.controller;

import group9.movie_reviews.service.impl.ReviewService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;

@Controller
@RequestMapping("/review")
@SessionAttributes("review")
public class ReviewController {

@Resource
private ReviewService reviewService;

@RequestMapping(method = RequestMethod.GET)
public String wtf(ModelMap model) {
model.addAttribute("a", reviewService.getReviews()); // NullPointerException here
return "index";
}

}


ReviewService:



package group9.movie_reviews.service;

import group9.movie_reviews.model.ReviewEntity;

public interface ReviewService {

Iterable<ReviewEntity> getReviews();

void addReview(String movieName);

}


ReviewServiceImpl:



package group9.movie_reviews.service.impl;

import group9.movie_reviews.model.ReviewEntity;
import group9.movie_reviews.repository.ReviewRepository;
import group9.movie_reviews.service.ReviewService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ReviewServiceImpl implements ReviewService {

@Resource
private ReviewRepository reviewRepository;

@Override
public Iterable<ReviewEntity> getReviews() {
return reviewRepository.findAll();
}

@Override
public void addReview(String movieName) {
ReviewEntity r = new ReviewEntity();
r.setMovieName(movieName);
reviewRepository.save(r);
}

}


ReviewRepository:



package group9.movie_reviews.repository;

import group9.movie_reviews.model.ReviewEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ReviewRepository extends JpaRepository<ReviewEntity, Integer> {
ReviewEntity findByMovieName(String movieName);
}


Stacktrace/error:



Type Exception Report

Message Request processing failed; nested exception is java.lang.NullPointerException

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Root Cause

java.lang.NullPointerException
group9.movie_reviews.controller.ReviewController.wtf(ReviewController.java:21)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


Config Files:



WebConfig:



package group9.movie_reviews.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"group9.movie_reviews"})
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"group9.movie_reviews.repository"})
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}

}


WebInit:



package group9.movie_reviews.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

@Configuration
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?> getRootConfigClasses() {
return new Class{RootConfig.class};
}

@Override
protected Class<?> getServletConfigClasses() {
return new Class{WebConfig.class};
}

@Override
protected String getServletMappings() {
return new String{"/", "/review"};
}

}


Redeploy exception/stacktrace:



[2018-11-22 02:53:37,857] Artifact movie_reviews:war exploded: Artifact is being deployed, please wait...
22-Nov-2018 02:53:37.878 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose Closing WebApplicationContext for namespace 'dispatcher-servlet': startup date [ Thu Nov 22 02:35:08 GMT 2018]; parent: Root WebApplicationContext
22-Nov-2018 02:53:37.878 WARNING [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose Exception thrown from LifecycleProcessor on context close
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Nov 22 02:35:08 GMT 2018]; parent: Root WebApplicationContext
at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:416)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:997)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:956)
at org.springframework.web.servlet.FrameworkServlet.destroy(FrameworkServlet.java:829)
at org.apache.catalina.core.StandardWrapper.unload(StandardWrapper.java:1393)
at org.apache.catalina.core.StandardWrapper.stopInternal(StandardWrapper.java:1729)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5573)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:851)
at org.apache.catalina.startup.HostConfig.unmanageApp(HostConfig.java:1755)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.removeContext(MBeanFactory.java:792)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
at sun.reflect.GeneratedMethodAccessor238.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:835)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

22-Nov-2018 02:53:37.880 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose Closing Root WebApplicationContext: startup date [Thu Nov 22 02:35:08 GMT 2018]; root of context hierarchy
22-Nov-2018 02:53:40.095 INFO [RMI TCP Connection(83)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
22-Nov-2018 02:53:40.197 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization started
22-Nov-2018 02:53:40.305 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.prepareRefresh Refreshing Root WebApplicationContext: startup date [Thu Nov 22 02:53:40 GMT 2018]; root of context hierarchy
22-Nov-2018 02:53:40.400 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.loadBeanDefinitions Registering annotated classes: [class group9.movie_reviews.config.RootConfig]
22-Nov-2018 02:53:40.709 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization completed in 512 ms
22-Nov-2018 02:53:40.713 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'dispatcher': initialization started
22-Nov-2018 02:53:40.719 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.prepareRefresh Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Nov 22 02:53:40 GMT 2018]; parent: Root WebApplicationContext
22-Nov-2018 02:53:40.722 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.loadBeanDefinitions Registering annotated classes: [class group9.movie_reviews.config.WebConfig]
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[2018-11-22 02:53:41,443] Artifact movie_reviews:war exploded: Artifact is deployed successfully
[2018-11-22 02:53:41,443] Artifact movie_reviews:war exploded: Deploy took 3,586 milliseconds


Pom.xml:



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>group9</groupId>
<artifactId>movie_reviews</artifactId>
<version>1.0</version>
<packaging>war</packaging>

<name>movie_reviews</name>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-version>4.3.3.RELEASE</spring-version>
<servlet-version>3.1.0</servlet-version>
<jstl-version>1.2</jstl-version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>

</project>









share|improve this question




















  • 1





    Let's see a stacktrace.

    – Sotirios Delimanolis
    Nov 22 '18 at 2:18











  • @SotiriosDelimanolis It's a NullPointerException :/ added anyway

    – user3278983
    Nov 22 '18 at 2:28











  • Making sure you're reading it right. Do you have a CommonAnnotationBeanPostProcessor bean registered to process the @Resource annotation?

    – Sotirios Delimanolis
    Nov 22 '18 at 2:31











  • @SotiriosDelimanolis I don't know what that is so I guess not. I have tried this with @Autowired annotations also, but intelliJ complains less with @Resource annotations. Still get the same error with @Autowired

    – user3278983
    Nov 22 '18 at 2:33











  • could you also attach the spring.config that you are using, xml or class config? Also as mentioned above , resource is used to define endpoints and not just services

    – AntJavaDev
    Nov 22 '18 at 2:39
















1












1








1








I'm trying to @Autowire a @Service in a @Controller. This results in a runtime NullPointerException (where I attempt to make use of the @Service). For some reason Spring isn't injecting the dependency and I'm yet to figure it out. It doesn't seem to be because of the two primary causes (manual instanstiation or lack of annotations).



ReviewController:



package group9.movie_reviews.controller;

import group9.movie_reviews.service.impl.ReviewService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;

@Controller
@RequestMapping("/review")
@SessionAttributes("review")
public class ReviewController {

@Resource
private ReviewService reviewService;

@RequestMapping(method = RequestMethod.GET)
public String wtf(ModelMap model) {
model.addAttribute("a", reviewService.getReviews()); // NullPointerException here
return "index";
}

}


ReviewService:



package group9.movie_reviews.service;

import group9.movie_reviews.model.ReviewEntity;

public interface ReviewService {

Iterable<ReviewEntity> getReviews();

void addReview(String movieName);

}


ReviewServiceImpl:



package group9.movie_reviews.service.impl;

import group9.movie_reviews.model.ReviewEntity;
import group9.movie_reviews.repository.ReviewRepository;
import group9.movie_reviews.service.ReviewService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ReviewServiceImpl implements ReviewService {

@Resource
private ReviewRepository reviewRepository;

@Override
public Iterable<ReviewEntity> getReviews() {
return reviewRepository.findAll();
}

@Override
public void addReview(String movieName) {
ReviewEntity r = new ReviewEntity();
r.setMovieName(movieName);
reviewRepository.save(r);
}

}


ReviewRepository:



package group9.movie_reviews.repository;

import group9.movie_reviews.model.ReviewEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ReviewRepository extends JpaRepository<ReviewEntity, Integer> {
ReviewEntity findByMovieName(String movieName);
}


Stacktrace/error:



Type Exception Report

Message Request processing failed; nested exception is java.lang.NullPointerException

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Root Cause

java.lang.NullPointerException
group9.movie_reviews.controller.ReviewController.wtf(ReviewController.java:21)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


Config Files:



WebConfig:



package group9.movie_reviews.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"group9.movie_reviews"})
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"group9.movie_reviews.repository"})
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}

}


WebInit:



package group9.movie_reviews.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

@Configuration
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?> getRootConfigClasses() {
return new Class{RootConfig.class};
}

@Override
protected Class<?> getServletConfigClasses() {
return new Class{WebConfig.class};
}

@Override
protected String getServletMappings() {
return new String{"/", "/review"};
}

}


Redeploy exception/stacktrace:



[2018-11-22 02:53:37,857] Artifact movie_reviews:war exploded: Artifact is being deployed, please wait...
22-Nov-2018 02:53:37.878 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose Closing WebApplicationContext for namespace 'dispatcher-servlet': startup date [ Thu Nov 22 02:35:08 GMT 2018]; parent: Root WebApplicationContext
22-Nov-2018 02:53:37.878 WARNING [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose Exception thrown from LifecycleProcessor on context close
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Nov 22 02:35:08 GMT 2018]; parent: Root WebApplicationContext
at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:416)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:997)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:956)
at org.springframework.web.servlet.FrameworkServlet.destroy(FrameworkServlet.java:829)
at org.apache.catalina.core.StandardWrapper.unload(StandardWrapper.java:1393)
at org.apache.catalina.core.StandardWrapper.stopInternal(StandardWrapper.java:1729)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5573)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:851)
at org.apache.catalina.startup.HostConfig.unmanageApp(HostConfig.java:1755)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.removeContext(MBeanFactory.java:792)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
at sun.reflect.GeneratedMethodAccessor238.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:835)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

22-Nov-2018 02:53:37.880 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose Closing Root WebApplicationContext: startup date [Thu Nov 22 02:35:08 GMT 2018]; root of context hierarchy
22-Nov-2018 02:53:40.095 INFO [RMI TCP Connection(83)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
22-Nov-2018 02:53:40.197 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization started
22-Nov-2018 02:53:40.305 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.prepareRefresh Refreshing Root WebApplicationContext: startup date [Thu Nov 22 02:53:40 GMT 2018]; root of context hierarchy
22-Nov-2018 02:53:40.400 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.loadBeanDefinitions Registering annotated classes: [class group9.movie_reviews.config.RootConfig]
22-Nov-2018 02:53:40.709 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization completed in 512 ms
22-Nov-2018 02:53:40.713 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'dispatcher': initialization started
22-Nov-2018 02:53:40.719 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.prepareRefresh Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Nov 22 02:53:40 GMT 2018]; parent: Root WebApplicationContext
22-Nov-2018 02:53:40.722 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.loadBeanDefinitions Registering annotated classes: [class group9.movie_reviews.config.WebConfig]
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[2018-11-22 02:53:41,443] Artifact movie_reviews:war exploded: Artifact is deployed successfully
[2018-11-22 02:53:41,443] Artifact movie_reviews:war exploded: Deploy took 3,586 milliseconds


Pom.xml:



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>group9</groupId>
<artifactId>movie_reviews</artifactId>
<version>1.0</version>
<packaging>war</packaging>

<name>movie_reviews</name>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-version>4.3.3.RELEASE</spring-version>
<servlet-version>3.1.0</servlet-version>
<jstl-version>1.2</jstl-version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>

</project>









share|improve this question
















I'm trying to @Autowire a @Service in a @Controller. This results in a runtime NullPointerException (where I attempt to make use of the @Service). For some reason Spring isn't injecting the dependency and I'm yet to figure it out. It doesn't seem to be because of the two primary causes (manual instanstiation or lack of annotations).



ReviewController:



package group9.movie_reviews.controller;

import group9.movie_reviews.service.impl.ReviewService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;

@Controller
@RequestMapping("/review")
@SessionAttributes("review")
public class ReviewController {

@Resource
private ReviewService reviewService;

@RequestMapping(method = RequestMethod.GET)
public String wtf(ModelMap model) {
model.addAttribute("a", reviewService.getReviews()); // NullPointerException here
return "index";
}

}


ReviewService:



package group9.movie_reviews.service;

import group9.movie_reviews.model.ReviewEntity;

public interface ReviewService {

Iterable<ReviewEntity> getReviews();

void addReview(String movieName);

}


ReviewServiceImpl:



package group9.movie_reviews.service.impl;

import group9.movie_reviews.model.ReviewEntity;
import group9.movie_reviews.repository.ReviewRepository;
import group9.movie_reviews.service.ReviewService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ReviewServiceImpl implements ReviewService {

@Resource
private ReviewRepository reviewRepository;

@Override
public Iterable<ReviewEntity> getReviews() {
return reviewRepository.findAll();
}

@Override
public void addReview(String movieName) {
ReviewEntity r = new ReviewEntity();
r.setMovieName(movieName);
reviewRepository.save(r);
}

}


ReviewRepository:



package group9.movie_reviews.repository;

import group9.movie_reviews.model.ReviewEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ReviewRepository extends JpaRepository<ReviewEntity, Integer> {
ReviewEntity findByMovieName(String movieName);
}


Stacktrace/error:



Type Exception Report

Message Request processing failed; nested exception is java.lang.NullPointerException

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Root Cause

java.lang.NullPointerException
group9.movie_reviews.controller.ReviewController.wtf(ReviewController.java:21)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


Config Files:



WebConfig:



package group9.movie_reviews.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"group9.movie_reviews"})
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"group9.movie_reviews.repository"})
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}

}


WebInit:



package group9.movie_reviews.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

@Configuration
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?> getRootConfigClasses() {
return new Class{RootConfig.class};
}

@Override
protected Class<?> getServletConfigClasses() {
return new Class{WebConfig.class};
}

@Override
protected String getServletMappings() {
return new String{"/", "/review"};
}

}


Redeploy exception/stacktrace:



[2018-11-22 02:53:37,857] Artifact movie_reviews:war exploded: Artifact is being deployed, please wait...
22-Nov-2018 02:53:37.878 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose Closing WebApplicationContext for namespace 'dispatcher-servlet': startup date [ Thu Nov 22 02:35:08 GMT 2018]; parent: Root WebApplicationContext
22-Nov-2018 02:53:37.878 WARNING [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose Exception thrown from LifecycleProcessor on context close
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Nov 22 02:35:08 GMT 2018]; parent: Root WebApplicationContext
at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:416)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:997)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:956)
at org.springframework.web.servlet.FrameworkServlet.destroy(FrameworkServlet.java:829)
at org.apache.catalina.core.StandardWrapper.unload(StandardWrapper.java:1393)
at org.apache.catalina.core.StandardWrapper.stopInternal(StandardWrapper.java:1729)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5573)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:851)
at org.apache.catalina.startup.HostConfig.unmanageApp(HostConfig.java:1755)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.removeContext(MBeanFactory.java:792)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
at sun.reflect.GeneratedMethodAccessor238.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:835)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

22-Nov-2018 02:53:37.880 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose Closing Root WebApplicationContext: startup date [Thu Nov 22 02:35:08 GMT 2018]; root of context hierarchy
22-Nov-2018 02:53:40.095 INFO [RMI TCP Connection(83)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
22-Nov-2018 02:53:40.197 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization started
22-Nov-2018 02:53:40.305 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.prepareRefresh Refreshing Root WebApplicationContext: startup date [Thu Nov 22 02:53:40 GMT 2018]; root of context hierarchy
22-Nov-2018 02:53:40.400 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.loadBeanDefinitions Registering annotated classes: [class group9.movie_reviews.config.RootConfig]
22-Nov-2018 02:53:40.709 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization completed in 512 ms
22-Nov-2018 02:53:40.713 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'dispatcher': initialization started
22-Nov-2018 02:53:40.719 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.prepareRefresh Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Nov 22 02:53:40 GMT 2018]; parent: Root WebApplicationContext
22-Nov-2018 02:53:40.722 INFO [RMI TCP Connection(83)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.loadBeanDefinitions Registering annotated classes: [class group9.movie_reviews.config.WebConfig]
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[2018-11-22 02:53:41,443] Artifact movie_reviews:war exploded: Artifact is deployed successfully
[2018-11-22 02:53:41,443] Artifact movie_reviews:war exploded: Deploy took 3,586 milliseconds


Pom.xml:



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>group9</groupId>
<artifactId>movie_reviews</artifactId>
<version>1.0</version>
<packaging>war</packaging>

<name>movie_reviews</name>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-version>4.3.3.RELEASE</spring-version>
<servlet-version>3.1.0</servlet-version>
<jstl-version>1.2</jstl-version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>

</project>






java spring hibernate spring-mvc spring-data-jpa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 13:08







user3278983

















asked Nov 22 '18 at 2:15









user3278983user3278983

3828




3828








  • 1





    Let's see a stacktrace.

    – Sotirios Delimanolis
    Nov 22 '18 at 2:18











  • @SotiriosDelimanolis It's a NullPointerException :/ added anyway

    – user3278983
    Nov 22 '18 at 2:28











  • Making sure you're reading it right. Do you have a CommonAnnotationBeanPostProcessor bean registered to process the @Resource annotation?

    – Sotirios Delimanolis
    Nov 22 '18 at 2:31











  • @SotiriosDelimanolis I don't know what that is so I guess not. I have tried this with @Autowired annotations also, but intelliJ complains less with @Resource annotations. Still get the same error with @Autowired

    – user3278983
    Nov 22 '18 at 2:33











  • could you also attach the spring.config that you are using, xml or class config? Also as mentioned above , resource is used to define endpoints and not just services

    – AntJavaDev
    Nov 22 '18 at 2:39
















  • 1





    Let's see a stacktrace.

    – Sotirios Delimanolis
    Nov 22 '18 at 2:18











  • @SotiriosDelimanolis It's a NullPointerException :/ added anyway

    – user3278983
    Nov 22 '18 at 2:28











  • Making sure you're reading it right. Do you have a CommonAnnotationBeanPostProcessor bean registered to process the @Resource annotation?

    – Sotirios Delimanolis
    Nov 22 '18 at 2:31











  • @SotiriosDelimanolis I don't know what that is so I guess not. I have tried this with @Autowired annotations also, but intelliJ complains less with @Resource annotations. Still get the same error with @Autowired

    – user3278983
    Nov 22 '18 at 2:33











  • could you also attach the spring.config that you are using, xml or class config? Also as mentioned above , resource is used to define endpoints and not just services

    – AntJavaDev
    Nov 22 '18 at 2:39










1




1





Let's see a stacktrace.

– Sotirios Delimanolis
Nov 22 '18 at 2:18





Let's see a stacktrace.

– Sotirios Delimanolis
Nov 22 '18 at 2:18













@SotiriosDelimanolis It's a NullPointerException :/ added anyway

– user3278983
Nov 22 '18 at 2:28





@SotiriosDelimanolis It's a NullPointerException :/ added anyway

– user3278983
Nov 22 '18 at 2:28













Making sure you're reading it right. Do you have a CommonAnnotationBeanPostProcessor bean registered to process the @Resource annotation?

– Sotirios Delimanolis
Nov 22 '18 at 2:31





Making sure you're reading it right. Do you have a CommonAnnotationBeanPostProcessor bean registered to process the @Resource annotation?

– Sotirios Delimanolis
Nov 22 '18 at 2:31













@SotiriosDelimanolis I don't know what that is so I guess not. I have tried this with @Autowired annotations also, but intelliJ complains less with @Resource annotations. Still get the same error with @Autowired

– user3278983
Nov 22 '18 at 2:33





@SotiriosDelimanolis I don't know what that is so I guess not. I have tried this with @Autowired annotations also, but intelliJ complains less with @Resource annotations. Still get the same error with @Autowired

– user3278983
Nov 22 '18 at 2:33













could you also attach the spring.config that you are using, xml or class config? Also as mentioned above , resource is used to define endpoints and not just services

– AntJavaDev
Nov 22 '18 at 2:39







could you also attach the spring.config that you are using, xml or class config? Also as mentioned above , resource is used to define endpoints and not just services

– AntJavaDev
Nov 22 '18 at 2:39














0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422983%2fspring-mvc-hibernate-autowired-service-in-controller-is-null%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422983%2fspring-mvc-hibernate-autowired-service-in-controller-is-null%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents