title: dataSource.getConnection throwing NullpointerException in Spring
body: <p>I am trying to connect to MySQL database in my spring application. I am able to connect to it if I use <code>DriverManager.getConnection(DB_URL,USER, PASS)</code> but unable to get connection with dataSource which is configured with bean.</p>
  
  <p>App context</p>
  
  <pre><code>&lt;bean id="jdbcTest" class="org.springdemo.jdbc.JdbcTest"&gt;
    &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
    &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"&gt;&lt;/property&gt;
    &lt;property name="url" value="jdbc:mysql://localhost:3306/test"&gt;&lt;/property&gt;
    &lt;property name="username" value="xxxx"&gt;&lt;/property&gt;
    &lt;property name="password" value="xxxx"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Class</p>
  
  <pre><code>public class JdbcTest {
  
    private DataSource dataSource;
  
    public JdbcTest(){
      try {
        Class.forName("com.mysql.jdbc.Driver");
        //conn = DriverManager.getConnection(DB_URL,USER,PASS); This worked!!!!
        conn = dataSource.getConnection(); 
        stmt = conn.createStatement();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  
    public ResultSet executeQuery(String query){ 
          //executing query; it works as I tested with DriverManager
    }
  
    public static void main(String[] args) {  
      AbstractApplicationContext context = 
        new ClassPathXmlApplicationContext("spring.xml");
      JdbcTest test = context.getBean("jdbcTest", JdbcTest.class);
      ResultSet rs = test.executeQuery("select * from employee");
      try {
        while(rs.next()){
          System.out.println(rs.getString(1));
        }
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  
  }
  </code></pre>
  
  <p>It worked perfectly fine when I use <code>DriverManager</code> but, when I use <code>DataSource</code> object to get connection which is configured with bean, it gives <code>NullPointerException</code></p>
  
  <pre><code>    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTest' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springdemo.jdbc.JdbcTest]: Constructor threw exception; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1011)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:957)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
      at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:139)
      at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:83)
      at org.springdemo.DrawingApp.main(DrawingApp.java:21)
  Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springdemo.jdbc.JdbcTest]: Constructor threw exception; nested exception is java.lang.NullPointerException
      at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1004)
      ... 13 more
  Caused by: java.lang.NullPointerException
      at org.springdemo.jdbc.JdbcTest.&lt;init&gt;(JdbcTest.java:47)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Unknown Source)
      at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
      ... 15 more
  </code></pre>