title: Vaadin and Spring - @Autowired field is null
body: <p>my <a href="https://stackoverflow.com/questions/24472459/vaadin-spring-integration-autowired-nullpointerexception">last question</a> was marked as duplicated, but answers not work for me :-(</p>
  
  <p>I have two classes in the same package and I need to find object from @Service and print result to log.</p>
  
  <pre><code>package com.example.test.springvaadin.vaadin;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  
  import com.example.test.springvaadin.entity.Role;
  import com.example.test.springvaadin.service.RoleManager;
  
  @Controller
  public class Test { 
      private static final Logger logger = LoggerFactory.getLogger(Test.class);
  
      @Autowired
      private RoleManager roleManager;
  
      @RequestMapping("/test")
      public void doTest() {
          Role role = roleManager.findById(1);
          logger.info("_ROLE_TEST_ " + role.toString());
          String text = role.getName();       
      }
  }
  </code></pre>
  
  <p>If I go to <em><a href="http://ip.ip.ip.ip:8080/springvaadin/test" rel="nofollow noreferrer">http://ip.ip.ip.ip:8080/springvaadin/test</a></em> I see in log (good):</p>
  
  <pre><code>14:27:09.126 [tomcat-http--4] INFO  c.e.test.springvaadin.vaadin.Test - _ROLE_TEST_ Role [id=1, name=Test]
  </code></pre>
  
  <p>There is second class:</p>
  
  <pre><code>package com.example.test.springvaadin.vaadin;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.vaadin.spring.VaadinUI;
  
  import com.example.test.springvaadin.entity.Role;
  import com.example.test.springvaadin.service.RoleManager;
  import com.vaadin.server.VaadinRequest;
  import com.vaadin.ui.Label;
  import com.vaadin.ui.UI;
  import com.vaadin.ui.VerticalLayout;
  
  @Controller // @Component do not work too
  @VaadinUI
  public class MainUI extends UI {
  
      private static final Logger logger = LoggerFactory.getLogger(MainUI.class);
  
      @Autowired
      private RoleManager roleManager;
  
      @Override
      protected void init(VaadinRequest request) {
          logger.info("MAINUI IN INIT");
          final VerticalLayout layout = new VerticalLayout();
          layout.setMargin(true);
          setContent(layout);
  
          Role role = roleManager.findById(1);
          logger.info("_ROLE_VAADIN_ " + role.toString());
          String text = role.getName();
  
          Label lblRoleName = new Label(text);
          layout.addComponent(lblRoleName);       
      }
  }
  </code></pre>
  
  <p>it is mapped by web.xml to <em>/ui</em>. At page <em><a href="http://ip.ip.ip.ip:8080/springvaadin/ui" rel="nofollow noreferrer">http://ip.ip.ip.ip:8080/springvaadin/ui</a></em> I get HTTP 500:</p>
  
  <pre><code>14:29:50.810 [tomcat-http--14] INFO  c.e.test.springvaadin.vaadin.MainUI - MAINUI IN INIT
  Čer 29, 2014 2:29:50 ODP. com.vaadin.server.DefaultErrorHandler doDefault
  SEVERE: 
  java.lang.NullPointerException
      at com.example.test.springvaadin.vaadin.MainUI.init(MainUI.java:40)
      at com.vaadin.ui.UI.doInit(UI.java:641)
      at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222)
      at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
      at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
      at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1405)
      at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:237)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  
  Čer 29, 2014 2:29:50 ODP. org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [MainUI] in context with path [/springvaadin] threw exception [com.vaadin.server.ServiceException: java.lang.NullPointerException] with root cause
  java.lang.NullPointerException
      at com.example.test.springvaadin.vaadin.MainUI.init(MainUI.java:40)
      at com.vaadin.ui.UI.doInit(UI.java:641)
      at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222)
      at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
      at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
      at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1405)
      at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:237)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>java.lang.NullPointerException
      at com.example.test.springvaadin.vaadin.MainUI.init(MainUI.java:40)</p>
  
  <p>but it is last line in class after last <em>}</em> in file.</p>
  
  <p>Spring knows where are clasees located:</p>
  
  <pre><code>&lt;context:component-scan base-package="com.example.test.springvaadin.controller" /&gt;
  &lt;context:component-scan base-package="com.example.test.springvaadin.dao" /&gt;
  &lt;context:component-scan base-package="com.example.test.springvaadin.service" /&gt;
  &lt;context:component-scan base-package="com.example.test.springvaadin.vaadin" /&gt;
  </code></pre>
  
  <p>What I do bad? Used annotation?</p>
  
  <p>Source code: <a href="https://dl.dropboxusercontent.com/u/6943408/vaadin-spring_.zip" rel="nofollow noreferrer">https://dl.dropboxusercontent.com/u/6943408/vaadin-spring_.zip</a></p>
  
  <hr>
  
  <p>Update 1:</p>
  
  <p>I remove @Controller annotation from <em>MainUI</em> class and now it crash on line 31 - "Role role = roleManager.findById(1);" There is error: </p>
  
  <pre><code>Čer 29, 2014 8:50:22 ODP. com.vaadin.server.DefaultErrorHandler doDefault
  SEVERE: 
  java.lang.NullPointerException
      at com.example.test.springvaadin.vaadin.MainUI.init(MainUI.java:31)
      at com.vaadin.ui.UI.doInit(UI.java:641)
      at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222)
      at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
      at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
      at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1405)
      at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:237)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  
  Čer 29, 2014 8:50:22 ODP. org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [MainUI] in context with path [/springvaadin] threw exception [com.vaadin.server.ServiceException: java.lang.NullPointerException] with root cause
  java.lang.NullPointerException
      at com.example.test.springvaadin.vaadin.MainUI.init(MainUI.java:31)
      at com.vaadin.ui.UI.doInit(UI.java:641)
      at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222)
      at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
      at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
      at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1405)
      at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:237)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>