title: java.lang.NullPointerException in mysql
body: <p>I;m trying to build the website that I found <a href="http://o7planning.org/en/10605/create-a-java-shopping-cart-web-application-using-spring-mvc-and-hibernate#a2564366" rel="nofollow noreferrer">here</a>, and when I run the app I get </p>
  
  <blockquote>
    <p>Error creating bean with name 'dataSource' defined in org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'getDataSource' threw exception; nested exception is java.lang.NullPointerException`</p>
  </blockquote>
  
  <p>I looked around my code and the problem seems to be regarding the fact that the app doesn't connect to the database. When I try and access the <a href="http://127.0.0.1:3306/paw" rel="nofollow noreferrer">http://127.0.0.1:3306/paw</a> it just tells me that the page isn't working.</p>
  
  <p>ds-hibernate-cfg.properties</p>
  
  <pre><code># DataSource
  
  ds.database-driver=com.mysql.jdbc.Driver
  ds.url=jdbc:mysql://127.0.0.1:3306/paw
  ds.username=root
  ds.password=
  
  
  # Hibernate Config
  
  hibernate.dialect=org.hibernate.dialect.MySQLDialect
  hibernate.show_sql=true
  current_session_context_class=thread
  hibernate.hbm2ddl.auto=update
  </code></pre>
  
  <p>ApplicationContextConfig.java</p>
  
  <pre><code>package org.o7planning.springmvcshoppingcart.config;
  
  import java.util.Properties;
  
  import javax.sql.DataSource;
  import com.mysql.jdbc.Driver;
  
  import org.hibernate.SessionFactory;
  import org.o7planning.springmvcshoppingcart.dao.impl.AccountDAO;
  import org.o7planning.springmvcshoppingcart.dao.impl.OrderDAO;
  import org.o7planning.springmvcshoppingcart.dao.impl.ProductDAO;
  import org.o7planning.springmvcshoppingcart.dao.impl.AccountDAOImpl;
  import org.o7planning.springmvcshoppingcart.dao.impl.OrderDAOImpl;
  import org.o7planning.springmvcshoppingcart.dao.impl.ProductDAOImpl;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.context.support.ResourceBundleMessageSource;
  import org.springframework.core.env.Environment;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  import org.springframework.orm.hibernate5.HibernateTransactionManager;
  import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  import org.springframework.web.multipart.commons.CommonsMultipartResolver;
  import org.springframework.web.servlet.view.InternalResourceViewResolver;
  
  @Configuration
  @ComponentScan("org.o7planning.springmvcshoppingcart.*")
  @EnableTransactionManagement
  // Load to Environment.
  @PropertySource("classpath:ds-hibernate-cfg.properties")
  public class ApplicationContextConfig {
  
      // The Environment class serves as the property holder
      // and stores all the properties loaded by the @PropertySource
  
      private Environment env;
  
      @Bean
      public ResourceBundleMessageSource messageSource() {
          ResourceBundleMessageSource rb = new ResourceBundleMessageSource();
          // Load property in message/validator.properties
          rb.setBasenames(new String[] { "messages/validator" });
          return rb;
      }
  
      @Bean(name = "viewResolver")
      public InternalResourceViewResolver getViewResolver() {
          InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
          viewResolver.setPrefix("/WEB-INF/pages/");
          viewResolver.setSuffix(".jsp");
          return viewResolver;
      }
  
      // Config for Upload.
      @Bean(name = "multipartResolver")
      public CommonsMultipartResolver multipartResolver() {
          CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
  
          // Set Max Size...
          // commonsMultipartResolver.setMaxUploadSize(...);
  
          return commonsMultipartResolver;
      }
  
      @Bean(name = "dataSource")
      public DataSource getDataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
  
          // See: ds-hibernate-cfg.properties
          dataSource.setDriverClassName(env.getProperty("ds.database-driver"));
          dataSource.setUrl(env.getProperty("ds.url"));
          dataSource.setUsername(env.getProperty("ds.username"));
          dataSource.setPassword(env.getProperty("ds.password"));
  
          System.out.println("## getDataSource: " + dataSource);
  
          return dataSource;
      }
  
  
      @Bean(name = "sessionFactory")
      public SessionFactory getSessionFactory(DataSource dataSource) throws Exception {
          Properties properties = new Properties();
  
          // See: ds-hibernate-cfg.properties
          properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
          properties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
          properties.put("current_session_context_class", env.getProperty("current_session_context_class"));
  
  
          LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
  
          // Package contain entity classes
          factoryBean.setPackagesToScan(new String[] { "org.o7planning.springmvcshoppingcart.entity" });
          factoryBean.setDataSource(dataSource);
          factoryBean.setHibernateProperties(properties);
          factoryBean.afterPropertiesSet();
          //
          SessionFactory sf = factoryBean.getObject();
          System.out.println("## getSessionFactory: " + sf);
          return sf;
      }
  
  
      @Bean(name = "transactionManager")
      public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
          HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
  
          return transactionManager;
      }
  
      @Bean(name = "accountDAO")
      public AccountDAO getApplicantDAO() {
          return new AccountDAOImpl();
      }
  
      @Bean(name = "productDAO")
      public ProductDAO getProductDAO() {
          return new ProductDAOImpl();
      }
  
      @Bean(name = "orderDAO")
      public OrderDAO getOrderDAO() {
          return new OrderDAOImpl();
      }
  
      @Bean(name = "accountDAO")
      public AccountDAO getAccountDAO()  {
          return new AccountDAOImpl();
  
  
  }
  </code></pre>
  
  <p>My full error log</p>
  
  <pre><code>SEVERE: Context initialization failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'getDataSource' threw exception; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4937)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      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:748)
  Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'getDataSource' threw exception; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
      ... 23 more
  Caused by: java.lang.NullPointerException
      at org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig.getDataSource(ApplicationContextConfig.java:72)
      at org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig$$EnhancerBySpringCGLIB$$9065b576.CGLIB$getDataSource$1(&lt;generated&gt;)
      at org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig$$EnhancerBySpringCGLIB$$9065b576$$FastClassBySpringCGLIB$$21772b0c.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
      at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:355)
      at org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig$$EnhancerBySpringCGLIB$$9065b576.getDataSource(&lt;generated&gt;)
      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.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
      ... 24 more
  
  May 25, 2017 8:41:08 PM org.apache.catalina.core.StandardContext listenerStart
  SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'getDataSource' threw exception; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4937)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      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:748)
  Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'getDataSource' threw exception; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
      ... 23 more
  Caused by: java.lang.NullPointerException
      at org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig.getDataSource(ApplicationContextConfig.java:72)
      at org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig$$EnhancerBySpringCGLIB$$9065b576.CGLIB$getDataSource$1(&lt;generated&gt;)
      at org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig$$EnhancerBySpringCGLIB$$9065b576$$FastClassBySpringCGLIB$$21772b0c.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
      at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:355)
      at org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig$$EnhancerBySpringCGLIB$$9065b576.getDataSource(&lt;generated&gt;)
      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.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
      ... 24 more
  </code></pre>