title: NullPointerException & Error creating bean with name 'entityManagerFactory' from Hibernate Configuration class (using spring data jpa)
body: <p>I have updated some dependency in my project after that my hibernate configuration class showing Nullpointerexception.</p>
  
  <p>I'm using spring data JPA repository with hibernate, it's been more than 24 hours still not found any appropriate solution regarding small problem.</p>
  
  <blockquote>
    <p>Some Solution which I have already tried:- </p>
    
    <ol>
    <li>Providing a bean name with @bean(name="entityManagerFactory")</li>
    <li>Providing a ref like @EnableJpaRepositories(basePackages = "com.carportal.repository", entityManagerFactoryRef
    ="entityManagerFactory")  </li>
    <li>By changing dialect, By changing DriverClassName, by changing datasource manager</li>
    </ol>
  </blockquote>
  
  <p><strong>Issue That I'm Facing</strong></p>
  
  <pre><code>19:29:11.756 [main] ERROR o.s.web.context.ContextLoader - Context initialization failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.carportal.config.HibernateConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:484)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:401)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:292)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4683)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5146)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
      at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
      at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
      at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:841)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
      at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
      at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
      at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
      at org.apache.catalina.core.StandardService.startInternal(StandardService.java:421)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
      at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:930)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
      at org.apache.catalina.startup.Catalina.start(Catalina.java:633)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:343)
      at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:474)
  Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.NullPointerException
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
      ... 45 common frames omitted
  Caused by: java.lang.NullPointerException: null
      at com.carportal.config.HibernateConfig.additionalProperties(HibernateConfig.java:74)
      at com.carportal.config.HibernateConfig.entityManagerFactory(HibernateConfig.java:46)
      at com.carportal.config.HibernateConfig$$EnhancerBySpringCGLIB$$f78c3ea8.CGLIB$entityManagerFactory$3(&lt;generated&gt;)
      at com.carportal.config.HibernateConfig$$EnhancerBySpringCGLIB$$f78c3ea8$$FastClassBySpringCGLIB$$e9aea17b.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
      at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
      at com.carportal.config.HibernateConfig$$EnhancerBySpringCGLIB$$f78c3ea8.entityManagerFactory(&lt;generated&gt;)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
      ... 46 common frames omitted
  </code></pre>
  
  <p><strong>pom.xml file</strong></p>
  
  <pre><code>&lt;properties&gt;
          &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
          &lt;spring.version&gt;5.2.6.RELEASE&lt;/spring.version&gt;
          &lt;springdatajpa.version&gt;2.2.6.RELEASE&lt;/springdatajpa.version&gt;
          &lt;spring.security.version&gt;5.3.1.RELEASE&lt;/spring.security.version&gt;
          &lt;mysqlconnector.version&gt;8.0.20&lt;/mysqlconnector.version&gt;
          &lt;hibernate.version&gt;5.4.15.Final&lt;/hibernate.version&gt;
          &lt;hibernate.validator&gt;6.1.5.Final&lt;/hibernate.validator&gt;
  &lt;/properties&gt;
      &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;${mysqlconnector.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-hikaricp&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-ehcache&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-java8&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate.validator&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.validator}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.cache&lt;/groupId&gt;
              &lt;artifactId&gt;cache-api&lt;/artifactId&gt;
              &lt;version&gt;1.1.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.ehcache&lt;/groupId&gt;
              &lt;artifactId&gt;ehcache&lt;/artifactId&gt;
              &lt;version&gt;3.8.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.zaxxer&lt;/groupId&gt;
              &lt;artifactId&gt;HikariCP&lt;/artifactId&gt;
              &lt;version&gt;3.4.5&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.javassist&lt;/groupId&gt;
              &lt;artifactId&gt;javassist&lt;/artifactId&gt;
              &lt;version&gt;3.27.0-GA&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;5.2.6.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-aspects&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
              &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
              &lt;version&gt;${springdatajpa.version}&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p><strong>HibernateConfig Class</strong></p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @PropertySource({ "classpath:db.properties" })
  @ComponentScan({ "com.carportal.model", "com.carportal.repository", "com.carportal" })
  @EnableJpaRepositories(basePackages = "com.carportal.repository")
  public class HibernateConfig {
  
      private final Logger logger = LoggerFactory.getLogger(this.getClass());
  
      @Autowired
      private static Environment env;
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
          logger.debug("Local Container Entity Manager Factory Bean");
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(dataSource());
          em.setPackagesToScan("com.carportal");
          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          em.setJpaProperties(additionalProperties());
          return em;
      }
  
      @Bean
      public DataSource dataSource() throws NamingException {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
          dataSource.setUrl("jdbc:mysql://localhost:3306/carportadb");
          dataSource.setUsername("sam");
          dataSource.setPassword("root");
          return dataSource;
      }
  
      @Bean
      public PlatformTransactionManager transactionManager() throws NamingException {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
          return transactionManager;
      }
  
      @Bean
      public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
          return new PersistenceExceptionTranslationPostProcessor();
      }
  
      Properties additionalProperties() {
          Properties hibernateProperties = new Properties();
          hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
          hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
          hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
          hibernateProperties.setProperty("hibernate.cache.region.factory_class",
                  env.getProperty("hibernate.cache.region.factory_class"));
          hibernateProperties.setProperty("hibernate.cache.use_query_cache",
                  env.getProperty("hibernate.cache.use_query_cache"));
          hibernateProperties.setProperty("hibernate.dialect.storage_engine",
                  env.getProperty("hibernate.dialect.storage_engine"));
  
          /*
           * hibernateProperties.put(CONNECTION_PROVIDER,
           * env.getProperty("hibernate.connection.provider_class"));
           */
  
          hibernateProperties.setProperty("hibernate.cache.use_second_level_cache",
                  env.getProperty("hibernate.cache.use_second_level_cache"));
  
          return hibernateProperties;
      }
  
  }
  </code></pre>
  
  <p><strong>db.properties</strong></p>
  
  <pre><code># jdbc
  jdbc.driverClassName=com.mysql.cj.jdbc.Driver
  jdbc.url=jdbc:mysql://localhost:3306/carportadb
  jdbc.user=sam
  jdbc.pass=root
  
  
  # hibernate
  hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
  hibernate.dialect.storage_engine=InnoDBStorageEngine
  hibernate.show_sql=true
  hibernate.hbm2ddl.auto=create
  
  hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
  #hibernate.connection.provider_class=org.hibernate.hikaricp.internal.HikariCPConnectionProvider
  hibernate.dialect.storage_engine=InnoDBStorageEngine
  hibernate.cache.use_second_level_cache=false
  hibernate.cache.use_query_cache=false
  </code></pre>