title: null pointer exception in hibernate session factory creation
body: <p>I am new in java and want to use hibernate but I am getting following error:</p>
  
  <pre><code>TEstting
  Jan 31, 2016 6:36:43 PM org.hibernate.cfg.Environment &lt;clinit&gt;
  INFO: Hibernate 3.2.1
  Jan 31, 2016 6:36:43 PM org.hibernate.cfg.Environment &lt;clinit&gt;
  INFO: hibernate.properties not found
  Jan 31, 2016 6:36:43 PM org.hibernate.cfg.Environment buildBytecodeProvider
  INFO: Bytecode provider name : cglib
  Jan 31, 2016 6:36:43 PM org.hibernate.cfg.Environment &lt;clinit&gt;
  INFO: using JDK 1.4 java.sql.Timestamp handling
  Jan 31, 2016 6:36:43 PM org.hibernate.cfg.Configuration configure
  INFO: configuring from resource: hibernate.cfg.xml
  Jan 31, 2016 6:36:43 PM org.hibernate.cfg.Configuration getConfigurationInputStream
  INFO: Configuration resource: hibernate.cfg.xml
  Jan 31, 2016 6:36:43 PM org.hibernate.cfg.Configuration addResource
  INFO: Reading mappings from resource : pojo.Contractor_Schedule__c
  java.lang.NullPointerException
      at org.apache.catalina.loader.WebappClassLoaderBase.findLoadedResource(WebappClassLoaderBase.java:3558)
      at org.apache.catalina.loader.WebappClassLoaderBase.getResourceAsStream(WebappClassLoaderBase.java:1633)
      at org.hibernate.cfg.Configuration.addResource(Configuration.java:557)
      at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1584)
      at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552)
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1531)
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1505)
      at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
      at Utility.HibernateUtility.&lt;clinit&gt;(HibernateUtility.java:15)
      at services.AccountServices.createAccount(AccountServices.java:33)
      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:497)
      at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
      at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
      at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
      at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
      at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
      at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
      at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
      at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
      at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
      at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
      at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
      at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
      at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
      at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
      at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at services.JaxRsFilterAuthentication.doFilter(JaxRsFilterAuthentication.java:31)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:745)
  Initial SessionFactory creation failed.java.lang.NullPointerException
  </code></pre>
  
  <p>Below is 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://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
   &lt;session-factory name="scrubbi"&gt;
    &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
    &lt;property name="hibernate.connection.password"&gt;aQGwrzids5kF&lt;/property&gt;
    &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://127.0.0.1:3306/scrubbi&lt;/property&gt;
    &lt;property name="hibernate.connection.username"&gt;admin8KJpCbh&lt;/property&gt;
    &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
    &lt;property name="hibernate.archive.autodetection"&gt;class&lt;/property&gt;
    &lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt;
    &lt;property name="hibernate.format_sql"&gt;true&lt;/property&gt;
    &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;
    &lt;mapping resource="pojo.Contractor_Schedule__c"/&gt;
   &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>Below is my HibernateUtility class</p>
  
  <pre><code>package Utility;
  
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  
  
  public class HibernateUtility {
  
      private static final SessionFactory sessionFactory;
  
      static {
              System.out.println("TEstting");
          try {
  
              sessionFactory = new Configuration().configure("hibernate.cfg.xml")
              .buildSessionFactory();
  
          } catch (Throwable ex) {
              ex.printStackTrace();
              System.err.println("Initial SessionFactory creation failed." + ex);
  
              throw new ExceptionInInitializerError(ex);
  
          }
  
      }
  
      public static SessionFactory getSessionFactory() {
  
          return sessionFactory;
  
      }
  }
  </code></pre>
  
  <p>Below is my pom.xml file</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;scrubbi&lt;/groupId&gt;
      &lt;artifactId&gt;scrubbi&lt;/artifactId&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;1.0&lt;/version&gt;
      &lt;name&gt;scrubbi&lt;/name&gt;
      &lt;repositories&gt;
          &lt;repository&gt;
              &lt;!-- The JBoss Public repository is a composite repository of several 
                  major repositories. See http://community.jboss.org/wiki/MavenGettingStarted-Users --&gt;
              &lt;id&gt;jcentral&lt;/id&gt;
              &lt;url&gt;https://repo1.maven.org/maven2/&lt;/url&gt;
              &lt;releases&gt;
                  &lt;enabled&gt;true&lt;/enabled&gt;
              &lt;/releases&gt;
              &lt;!-- You can disable snapshot resolution to speed up your builds --&gt;
              &lt;snapshots&gt;
                  &lt;enabled&gt;false&lt;/enabled&gt;
              &lt;/snapshots&gt; 
          &lt;/repository&gt;
      &lt;/repositories&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;maven.compiler.source&gt;1.6&lt;/maven.compiler.source&gt;
          &lt;maven.compiler.target&gt;1.6&lt;/maven.compiler.target&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.transaction&lt;/groupId&gt;
              &lt;artifactId&gt;jta&lt;/artifactId&gt;
              &lt;version&gt;1.1&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;5.0.0.Final&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;5.0.0.Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-annotations&lt;/artifactId&gt;
              &lt;version&gt;3.5.6-Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-commons-annotations&lt;/artifactId&gt;
              &lt;version&gt;3.3.0.ga&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt; 
              &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
              &lt;version&gt;1.5.5&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.jboss.logging&lt;/groupId&gt;
              &lt;artifactId&gt;jboss-logging&lt;/artifactId&gt; 
              &lt;version&gt;3.3.0.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt; 
              &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
              &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
              &lt;version&gt;9.2-1003-jdbc4&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;5.1.25&lt;/version&gt;
          &lt;/dependency&gt;     
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
              &lt;version&gt;3.0.1&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;asm&lt;/groupId&gt;
              &lt;artifactId&gt;asm&lt;/artifactId&gt;
              &lt;version&gt;3.3.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
              &lt;version&gt;1.19&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
              &lt;version&gt;1.19&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-core&lt;/artifactId&gt;
              &lt;version&gt;1.19&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-json&lt;/artifactId&gt;
              &lt;version&gt;1.19&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-servlet&lt;/artifactId&gt;
              &lt;version&gt;1.19&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
              &lt;version&gt;2.5&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
              &lt;version&gt;2.5&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
             &lt;groupId&gt;com.owlike&lt;/groupId&gt;
             &lt;artifactId&gt;genson&lt;/artifactId&gt;
             &lt;version&gt;1.3&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet.jsp-api&lt;/artifactId&gt;
              &lt;version&gt;2.3.1&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;                       
      &lt;/dependencies&gt;
      &lt;profiles&gt;
          &lt;profile&gt;
              &lt;!-- When built in OpenShift the 'openshift' profile will be used when 
                  invoking mvn. --&gt;
              &lt;!-- Use this profile for any OpenShift specific customization your app 
                  will need. --&gt;
              &lt;!-- By default that is to put the resulting archive into the 'webapps' 
                  folder. --&gt;
              &lt;!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html --&gt;
              &lt;id&gt;openshift&lt;/id&gt;
              &lt;build&gt;
                  &lt;finalName&gt;scrubbi&lt;/finalName&gt;
                  &lt;plugins&gt;
                      &lt;plugin&gt;
                          &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                          &lt;version&gt;2.1.1&lt;/version&gt;
                          &lt;configuration&gt;
                              &lt;outputDirectory&gt;webapps&lt;/outputDirectory&gt;
                              &lt;warName&gt;ROOT&lt;/warName&gt;
                          &lt;/configuration&gt;
                      &lt;/plugin&gt;
                  &lt;/plugins&gt;
              &lt;/build&gt;
          &lt;/profile&gt;
      &lt;/profiles&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>And below is the code from where I am calling session factory</p>
  
  <pre><code>package services;
  
  import java.util.List;
  
  import javax.ws.rs.Consumes;
  import javax.ws.rs.GET;
  import javax.ws.rs.POST;
  import javax.ws.rs.Path;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.Response;
  import javax.ws.rs.core.Response.Status;
  
  import org.hibernate.Session;
  import org.hibernate.Transaction;
  
  import Utility.HibernateUtility;
  import pojo.Account;
  import pojo.Contractor_Schedule__c;
  import pojo.PersonAccount;
  
  @Path("/PersonAccount")
  public class AccountServices {
  
      @Path("/create")
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.APPLICATION_JSON)
      public Response createAccount(List&lt;PersonAccount&gt; records) throws Exception {
  
          //throw new WebApplicationException(Response.status(400).entity("This is invalid").build());
          Session session = HibernateUtility.getSessionFactory().openSession();
          Transaction tx = session.beginTransaction();
          for(PersonAccount record : records){
              session.saveOrUpdate(record);
          }
          tx.commit();
          session.close();
          return Response.status(Status.CREATED).entity(records).build();
      }
  
  
  }
  </code></pre>
  
  <p>Below is project structure image</p>
  
  <p><a href="https://i.stack.imgur.com/pI37S.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pI37S.png" alt="enter image description here"></a></p>
  
  <p>Is there any configuration problem or I am doing completely wrong?</p>