title: Null pointer exception when using @Autowired annotation to inject bean
body: <hr>
  
  <p>When I use @Autowired annotation to inject dao to service,java.lang.NullPointerException occurred.I can't fix it.Please anyone help!</p>
  
  <p><strong>exception</strong></p>
  
  <pre><code>java.lang.NullPointerException
  at com.sargeras.login.service.LoginServiceImpl.register(LoginServiceImpl.java:14)
  </code></pre>
  
  <p><strong>service</strong></p>
  
  <pre><code>public class LoginServiceImpl implements LoginService{
  @Autowired
  private LoginDao loginDao;
  public int register(UserVo userVo){
      return loginDao.register(userVo);
  }
  }
  </code></pre>
  
  <p><strong>dao</strong></p>
  
  <pre><code>@Repository
  public class LoginDaoImpl extends JdbcDaoSupport implements LoginDao {
  @Override
  public int register(UserVo userVo) {
      int result = -1;
      return result;
  }
  </code></pre>
  
  <p><strong>springmvc.xml</strong></p>
  
  <pre><code>&lt;context:component-scan base-package="com.sargeras" /&gt;
  &lt;mvc:annotation-driven /&gt;
  </code></pre>
  
  <p><strong>applicationContext.xml</strong></p>
  
  <pre><code>&lt;bean name="loginService" class="com.sargeras.login.service.LoginServiceImpl"&gt;
  &lt;/bean&gt;
  &lt;bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close"&gt;
      &lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&gt;
      &lt;property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" /&gt;
      &lt;property name="username" value="sargeras" /&gt;
      &lt;property name="password" value="sargeras" /&gt;
  &lt;/bean&gt;
  &lt;bean name="loginDao" class="com.sargeras.login.repository.LoginDaoImpl"&gt;
      &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>controller</strong></p>
  
  <pre><code>@Controller
  @RequestMapping(value = "/login")
  public class LoginController extends BaseController{
  @Autowired
  private LoginService service;
  @RequestMapping(value = "validate")
  public void toLogin(HttpServletResponse response) throws Exception {
      logger.error("hhhh");
      response.getWriter().print(" this is to logging 1");
  }
  @RequestMapping(value="register")
  public void register(HttpServletRequest request,HttpServletResponse response) throws   Exception{
      UserVo user = new UserVo();
      int result = 0;
      String userName = (String)request.getParameter("usernamesignup");
      String userPassword = (String)request.getParameter("passwordsignup_confirm");
      String userEmail = (String)request.getParameter("emailsignup");
      user.setUserName(userName);
      user.setUserPassword(userPassword);
      user.setUserEmail(userEmail);
      result = service.register(user);
      if(result == 1){
          response.getWriter().print("Success");
      }
  }
  }
  </code></pre>
  
  <p>service is used in controller and can be injected successfully</p>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  id="WebApp_ID" version="2.5"&gt;
  &lt;display-name&gt;spring_test&lt;/display-name&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;classpath:/config/applicationContext*.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;classpath:log4j.xml&lt;/param-value&gt;
   &lt;/context-param&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;filter&gt;
      &lt;filter-name&gt;characterEncodingFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;encoding&lt;/param-name&gt;
          &lt;param-value&gt;UTF-8&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;characterEncodingFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;listener&gt;
  &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath:/config/spring-servlet.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>** this is my web.xml configuration**</p>
  
  <p><strong>**problem resolved as follows **</strong></p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath:/config/spring-servlet.xml,
          classpath:/config/applicationContext*.xml
          &lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/servlet&gt;
  </code></pre>
  
  <p>Application context and servlet context can not be mixed,I'll keep that in mind.Thank you all!</p>