title: NullPointerException after a try to access database via Hibernate
body: <p>I try to develop a simple web application, but to exclude all the possibilities of a mistake in the web front-end, I have written a little class which I execute from a command prompt.</p>
  
  <pre><code>package org.vadim.testmvc;
  
  import org.vadim.testmvc.model.*;
  import org.vadim.testmvc.service.*;
  public class TestMain {
      public static void main(String[] args){
          Strings s = new Strings();
          TestService ts = new TestService();
          s.setText("StrText");
          ts.addStrings(s);
          System.out.println(ts.listStrings());
      }
  }
  </code></pre>
  
  <p>TestService.java (without import statements but be sure they are present in the original listing):</p>
  
  <pre><code>package org.vadim.testmvc.service;
  
  @Service
  public class TestService implements TestServiceInterface {
  
      //@Autowired
      TestDAO testdao = new TestDAO();
  
      @Transactional
      public List&lt;Strings&gt; listStrings(){
          return testdao.listStrings();
      }
  
      @Transactional
      public void addStrings(Strings strings){
          testdao.addStrings(strings);
      }
  }
  </code></pre>
  
  <p>TestDAO.java:</p>
  
  <pre><code>package org.vadim.testmvc.dao;
  
  @Transactional
  @Repository
  public class TestDAO implements TestDAOInterface {
  
      @Autowired
      private SessionFactory sessionFactory;
  
      public void addStrings(Strings strings){
          sessionFactory.getCurrentSession().save(strings);
      }
  
      @SuppressWarnings("unchecked")
      public List&lt;Strings&gt; listStrings(){
           return sessionFactory.getCurrentSession().createQuery("from Strings").list();
      }
  }
  </code></pre>
  
  <p>Entity class:</p>
  
  <pre><code>package org.vadim.testmvc.model;
  
  @Entity
  @Table(name="STRINGS")
  public class Strings {
  
      @Id
      @Column(name="ID")
      @GeneratedValue(strategy=GenerationType.AUTO)
      private Integer id;
  
      @Column(name="TEXT")
      private String text;
  
      public void setId(Integer id){
          this.id=id;
      }
  
      public Integer getId(){
          return id;
      }
  
      public String getText(){
          return text;
      }
  
      public void setText(String text){
          this.text=text;
      }   
  }
  </code></pre>
  
  <p>hibernate.cfg.xml:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
  &lt;mapping class="org.vadim.testmvc.model.Strings"/&gt;
  &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>root-context.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&gt;
  
  &lt;context:annotation-config/&gt;
  &lt;context:component-scan base-package="org.vadim.testmvc.dao"/&gt;
  &lt;context:component-scan base-package="org.vadim.testmvc.service"/&gt;
  &lt;import resource="data.xml"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>data.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
  &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
  &lt;property name="basename" value="classpath:messages"/&gt;
  &lt;property name="defaultEncoding" value="UTF-8"/&gt;
  &lt;/bean&gt;
  
      &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.properties"/&gt;
  
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}"/&gt;
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="configLocation"&gt;
      &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="configurationClass"&gt;
      &lt;value&gt;org.hibernate.cfg.AnnotationConfiguration&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
      &lt;props&gt;
      &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
      &lt;prop key="hibernate.dialect"&gt;${jdbc.dialect}&lt;/prop&gt;
      &lt;prop key="hibernate.connection.charSet"&gt;UTF-8&lt;/prop&gt;
      &lt;/props&gt;
      &lt;/property&gt;
      &lt;/bean&gt;
      &lt;/beans&gt;
  </code></pre>
  
  <p>This is the stack trace after the exception encounters:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at org.vadim.testmvc.dao.TestDAO.listStrings(TestDAO.java:23)
      at org.vadim.testmvc.service.TestService.listStrings(TestService.java:18)
      at org.vadim.testmvc.TestMain.main(TestMain.java:11)
  </code></pre>
  
  <p>The same happens if I try to execute TestDAO.addStrings(Strings strings) method, that is to say that the problem is not in the return result of listStrings(), but in the access to the database. I have tried either to add some entries from mysql command prompt, to make it not empty. I succeed to do that but that didn't influence the behaviour of the programm.</p>
  
  <p>Here is what I get if just declare <code>TestService testservice;</code> with @Autowire annotation</p>
  
  <pre><code>ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.vadim.testmvc.dao.TestDAO org.vadim.testmvc.service.TestService.testdao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.vadim.testmvc.dao.TestDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
      at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)
      at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
      at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
      at java.util.concurrent.FutureTask.run(FutureTask.java:166)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.vadim.testmvc.dao.TestDAO org.vadim.testmvc.service.TestService.testdao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.vadim.testmvc.dao.TestDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
      ... 21 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.vadim.testmvc.dao.TestDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
      ... 23 more
  </code></pre>