title: java.lang.NullPointerException Hibernate used with Ehcache
body: <p>I used Hibernate 4.1.2 with Ehcache 2.4.3 (shipped together with hibernate when donwloaded hibernate).</p>
  
  <p>My hibernate.cfg.xml :</p>
  
  <pre><code> &lt;?xml version='1.0' encoding='utf-8'?&gt;
    &lt;!DOCTYPE hibernate-configuration PUBLIC
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
   &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
            &lt;property name="hibernate.connection.driver_class"&gt;  com.microsoft.sqlserver.jdbc.SQLServerDriver &lt;/property&gt;
            &lt;property name="hibernate.connection.url"&gt; jdbc:sqlserver://localhost:1433;databaseName=Stock_indices&lt;/property&gt;
            &lt;property name="hibernate.connection.username"&gt;xxx&lt;/property&gt;
            &lt;property name="hibernate.connection.password"&gt;xxx&lt;/property&gt;
            &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;
            &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.SQLServerDialect&lt;/property&gt;
            &lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt;
            &lt;property name="hibernate.c3p0.min_size"&gt;5&lt;/property&gt;
            &lt;property name="hibernate.c3p0.max_size"&gt;20&lt;/property&gt;
            &lt;property name="hibernate.c3p0.timeout"&gt;300&lt;/property&gt;
            &lt;property name="hibernate.c3p0.max_statements"&gt;50&lt;/property&gt;
            &lt;property name="hibernate.c3p0.idle_test_period"&gt;30&lt;/property&gt; 
  
  
            &lt;property name="hibernate.cache.use_second_level_cache"&gt;true&lt;/property&gt;
            &lt;property name="hibernate.cache.use_query_cache"&gt;true&lt;/property&gt;
            &lt;property  name="hibernate.cache.region.factory_class"&gt;org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory              &lt;/property&gt;
            &lt;property name="net.sf.ehcache.configurationResourceName"&gt;ehcache-entity.xml&lt;/property&gt;
  
            &lt;mapping resource="mapping.xml"/&gt;
  
        &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>ehcache-entity.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
     &lt;ehcache&gt;
  
         &lt;cache name="stockdata.StockDatabaseConnection" eternal="false"
          maxElementsInMemory="5" overflowToDisk="true" diskPersistent="false"
          timeToIdleSeconds="0" timeToLiveSeconds="300"
          memoryStoreEvictionPolicy="LRU" /&gt;
  
     &lt;/ehcache&gt;   
  </code></pre>
  
  <p>mapping.xml</p>
  
  <pre><code>   &lt;?xml version="1.0"?&gt;
    &lt;!DOCTYPE hibernate-mapping PUBLIC
         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;    
  
         &lt;hibernate-mapping package="stockdata"&gt;
  
              &lt;class name="StockDatabaseConnection" table="STOCKINDEX"&gt; 
  
                 &lt;cache usage="read-only" /&gt;
                    &lt;composite-id name="CompositeIDConnection"&gt;
              &lt;key-property name="ticker" column="TICKER"/&gt;
              &lt;key-property name="indexdate" column="INDEXDATE"/&gt;
            &lt;/composite-id&gt;
  
                    &lt;property name="openprice"&gt; &lt;column name="OPENPRICE" /&gt; &lt;/property&gt;
                    &lt;property name="closingprice"&gt; &lt;column name="CLOSEPRICE" /&gt; &lt;/property&gt;
                    &lt;property name="highestprice"&gt; &lt;column name="HIGHPRICE" /&gt; &lt;/property&gt;
                    &lt;property name="lowestprice"&gt; &lt;column name="LOWPRICE" /&gt; &lt;/property&gt;
                    &lt;property name="volume"&gt; &lt;column name="VOLUME" /&gt; &lt;/property&gt;
  
  
              &lt;/class&gt;
       &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>However, I got this exception :</p>
  
  <pre><code>     java.lang.NullPointerException at   org.hibernate.cache.ehcache.internal.util.HibernateUtil.loadAndCorrectConfiguration(HibernateUtil.java:64)
    at  org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory.start(SingletonEhCacheRegionFactory.java:91)
   at org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:281)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1741)
            ............
  </code></pre>
  
  <p>It seems that hibernate can not load configuration? How can I solve the problem? (The serlvet works fine without using Ehcache).</p>
  
  <p>My servlet class :</p>
  
  <pre><code>package stockdataservlet;
  
  import java.io.IOException;
  import java.math.BigDecimal;
  import java.util.Iterator;
  import java.util.List;
  
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.hibernate.Query;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.Transaction;
  import org.hibernate.cfg.Configuration;
  import org.hibernate.service.ServiceRegistry;
  import org.hibernate.service.ServiceRegistryBuilder;
  
  import com.google.gson.Gson;
  import com.microsoft.sqlserver.jdbc.*;
  
  
   public class DataServlet extends HttpServlet {
       private static final long serialVersionUID = 1L;
  
       public static SessionFactory sessionfactory = null;
       public void init() {
           try {
               Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
               Configuration conf = new Configuration();
               conf.configure();
               ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();   
               sessionfactory = conf.buildSessionFactory(serviceRegistry);
  
                 }
         catch(Exception e){
  
          e.printStackTrace();
                 }
                 }
  
       protected void doGet(HttpServletRequest request, HttpServletResponse response) {
            Session session = null;
         try{
             session = sessionfactory.openSession();
             Transaction transaction = session.beginTransaction();
             Query query = session.createSQLQuery("SELECT * FROM STOCKINDEX WHERE TICKER = :index ").addScalar("CLOSEPRICE");
             query.setParameter("index", "AAA");
             List list = query.list();
             transaction.commit();
             String json = new Gson().toJson(list);
             /*response.setContentType("application/json");
             response.setCharacterEncoding("UTF-8");
             response.getWriter().write(json);*/
           }
         catch(Exception e){
          e.printStackTrace();
  
         }
           finally{
  
          session.close();
         }    
  }
  </code></pre>
  
  <p>}
  <img src="https://i.stack.imgur.com/GkTKz.png" alt="enter image description here"></p>