title: HibernateSearch ElasticSearch integration error : Exception in thread "main" java.lang.ExceptionInInitializerError
body: <p>Am trying to configure HibernateSearch for ElasticSearch Integration. Am getting the below error from HibernateSearch :</p>
  
  <p>Am using Oracle database and added the required dependencies in my pom.xml file</p>
  
  <p>Please find my error am getting : </p>
  
  <pre><code>Exception in thread "main" java.lang.ExceptionInInitializerError
  at com.test.webservice.elasticsearch.App.displayContactTableData(App.java:50)
  at com.test.webservice.elasticsearch.App.main(App.java:70)
  Caused by: java.lang.NullPointerException
  at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.&lt;init&gt;(MetadataBuilderImpl.java:579)
  at org.hibernate.boot.internal.MetadataBuilderImpl.&lt;init&gt;(MetadataBuilderImpl.java:127)
  at org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:135)
  at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:654)
  at com.test.webservice.elasticsearch.HibernateUtil.configureSessionFactory(HibernateUtil.java:36)
  at com.test.webservice.elasticsearch.HibernateUtil.&lt;clinit&gt;(HibernateUtil.java:44)
  ... 2 more
  </code></pre>
  
  <p>Its throwing error in HibernateUtil.java for the below line :</p>
  
  <pre><code>    sessionFactory = configuration.buildSessionFactory(serviceRegistry);  
  </code></pre>
  
  <p>Am using all the latest dependencies.</p>
  
  <pre><code>hibernate-search-orm -&gt;5.9.1.Final  
  hibernate-core -&gt;5.2.16.Final 
  ojdbc14 -&gt; 10.2.0.4.0
  </code></pre>
  
  <p>hibernate.cfg.xml</p>
  
  <pre><code>&lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
  
      &lt;property name="hibernate.connection.driver_class"&gt;oracle.jdbc.driver.OracleDriver&lt;/property&gt;
      &lt;property name="hibernate.connection.url"&gt;jdbc:oracle:thin:@localhost:1521:xe&lt;/property&gt;
      &lt;property name="hibernate.connection.username"&gt;testdb&lt;/property&gt;
      &lt;property name="hibernate.connection.password"&gt;123456&lt;/property&gt;
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.OracleDialect&lt;/property&gt;
      &lt;property name="show_sql"&gt;false&lt;/property&gt;
      &lt;property name="format_sql"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt;
      &lt;property name="hibernate.search.default.directory_provider"&gt;filesystem&lt;/property&gt;
      &lt;property name="hibernate.search.default.indexBase"&gt;C:\lucene\product\indexes&lt;/property&gt;
  
      &lt;mapping class="com.test.webservice.model.Product" /&gt;
  
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>HibernateUtil.java</p>
  
  <pre><code>import org.hibernate.HibernateException;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  import org.hibernate.service.ServiceRegistry;
  import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  
      public class HibernateUtil {
  
          private static SessionFactory sessionFactory = null;  
          private static ServiceRegistry serviceRegistry = null;  
  
          private static SessionFactory configureSessionFactory() throws HibernateException {  
          Configuration configuration = new Configuration();  
          configuration.configure();  
  
          Properties properties = configuration.getProperties();
          ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(properties).build();
          sessionFactory = configuration.buildSessionFactory(serviceRegistry);  
  
          return sessionFactory;  
      }
      static {
          configureSessionFactory();
      }
  
      private HibernateUtil() {}
  
      public static Session getSession() {
          return sessionFactory.openSession();
      }
  }
  </code></pre>
  
  <p>App.java</p>
  
  <pre><code>public class App 
  {
      private static void doIndex() throws InterruptedException {
          Session session = HibernateUtil.getSession();
  
          FullTextSession fullTextSession = Search.getFullTextSession(session);
          fullTextSession.createIndexer().startAndWait();
  
          fullTextSession.close();
      }
  
      private static List&lt;Product&gt; search(String queryString) {
          Session session = HibernateUtil.getSession();
          FullTextSession fullTextSession = Search.getFullTextSession(session);
  
          QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Product.class).get();
          org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("name").matching(queryString).createQuery();
  
          // wrap Lucene query in a javax.persistence.Query
          org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, Product.class);
  
          List&lt;Product&gt; productList = fullTextQuery.list();
  
          fullTextSession.close();
  
          return productList;
      }
  
      private static void displayContactTableData() {
          Session session = null;
  
          try {
              session = HibernateUtil.getSession();
  
              // Fetching saved data
              List&lt;Product&gt; productList = session.createQuery("from Product").list();
  
              for (Product product : productList) {
                  System.out.println(product);
              }
  
          } catch (Exception ex) {
              ex.printStackTrace();
          } finally{
              if(session != null) {
                  session.close();
              }
          }
      }
  
      public static void main(String[] args) throws InterruptedException {
          System.out.println("\n\n******Data stored in Contact table******\n");
          displayContactTableData();
  
          // Create an initial Lucene index for the data already present in the database
          doIndex();
  
          Scanner scanner = new Scanner(System.in);
          String consoleInput = null;
  
          while (true) {
              // Prompt the user to enter query string
              System.out.println("\n\nEnter search key (To exit type 'X')");      
              System.out.println();
              consoleInput = scanner.nextLine();
  
              if("X".equalsIgnoreCase(consoleInput)) {
                  System.out.println("End");
                  System.exit(0);
              }   
  
              List&lt;Product&gt; result = search(consoleInput);            
              System.out.println("\n\n&gt;&gt;&gt;&gt;&gt;&gt;Record found for '" + consoleInput + "'");
  
              for (Product product : result) {
                  System.out.println(product);
              }               
          }           
      }
  }
  </code></pre>