title: java.lang.NullPointerException in sessionfactory hibernate
body: <p>I'm trying to build an application using hibernate. The thing is, sessionFactory can't open a session because there is a NullPointerException at <code>Session session = sessionFactory.openSession();</code> and that's why at <code>myAccount.addElement("August", 50000, 14000, 10000, 10000, 16000);</code> too.</p>
  
  <p>Main class </p>
  
  <pre><code>package myaccount1;
  import myaccount.entity1.*;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.Transaction;
  import org.hibernate.cfg.Configuration;
  import java.util.List;
  import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  import org.hibernate.service.ServiceRegistry;
  
  public class MyAccount {
  
  
  private static SessionFactory sessionFactory;
  private static ServiceRegistry serviceRegistry;
  
  public static SessionFactory createSessionFactory() {
      Configuration configuration = new Configuration();
      configuration.configure();
      ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
              configuration.getProperties()).build();
      sessionFactory = configuration.buildSessionFactory(serviceRegistry);
      return sessionFactory;
  }
  
  public static void main(String[] args) {
  
          MyAccount myAccount = new MyAccount();
  
          System.out.println("Adding an element");
  
          myAccount.addElement("August", 50000, 14000, 10000, 10000, 16000);
          myAccount.addElement("September", 50000, 14000, 10000, 10000, 16000);
  
          System.out.println("List of elements");
  
  
  
      }
  
  public void addElement(String mounth, int salary, int must_save_for_rent, int must_save_for_education, int must_save, int balance) {
      Session session = sessionFactory.openSession();
      Transaction transaction = null;
  
      try {
          transaction = session.beginTransaction();
          MyAccountEntity element = new MyAccountEntity(mounth, salary, must_save_for_rent, must_save_for_education, must_save, balance);
          session.save(element);
          transaction.commit();
      } catch (Exception e) {
          if (transaction != null) {
              transaction.rollback();
              e.printStackTrace();
          }
          e.printStackTrace();
      } finally {
          session.close();
      }
  
  }
  }
  </code></pre>
  
  <p>Configuration file </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&gt;
  &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
  &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
  &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/app?zeroDateTimeBehavior=convertToNull&lt;/property&gt;
  &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
  &lt;property name="hibernate.connection.password"&gt;sopromat&lt;/property&gt;
  &lt;mapping class="myaccount.entity1.MyAccountEntity.hbm.xml"/&gt;
  &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>HibernateUtil class</p>
  
  <pre><code>import org.hibernate.SessionFactory;
  import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  import org.hibernate.cfg.Configuration;
  import org.hibernate.service.ServiceRegistry;
  
  
  public class HibernateUtil {
      private static SessionFactory sessionFactory = 
              buildSessionFactory();
      private static ServiceRegistry serviceRegistry;
  
      private static SessionFactory buildSessionFactory() {
          try {
  
              Configuration configuration = new Configuration();
              configuration.configure();
              //Use config file path explicitly 
              //configuration.configure("hibernate/hibernate.cfg.xml");
              serviceRegistry = new StandardServiceRegistryBuilder()
              .applySettings(configuration.getProperties()).build();
              sessionFactory = configuration
                      .buildSessionFactory(serviceRegistry);
              return sessionFactory;
  
          } catch (Throwable ex) {
              System.err.println("Initial SessionFactory "
                      + "creation failed." + ex);
              throw new ExceptionInInitializerError(ex);
          }
      }
  
      public static SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      public static void shutdown() {
          // Close caches and connection pools
          getSessionFactory().close();
      }
  
  }
  </code></pre>
  
  <p>Mapping file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&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&gt;
    &lt;class name="myaccount.entity1.MyAccountEntity" table="myaccount"&gt;
        &lt;id name="id" type="int" column="id"&gt;
              &lt;generator class="auto_increment"/&gt;
          &lt;/id&gt;
          &lt;property name="mounth" column="mounth" type="string"/&gt;
          &lt;property name="salary" column="salary" type="int"/&gt;
          &lt;property name="must_save_for_rent" column="must_save_for_rent" type="int"/&gt;
          &lt;property name="must_save_for_education" column="must_save_for_education" type="int"/&gt;
          &lt;property name="must_save" column="must_save" type="int"/&gt;
          &lt;property name="balance" column="balance" type="int"/&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Project structure 
  <a href="https://i.stack.imgur.com/v86cS.jpg" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>What's wrong? 
  Please help, i don't know what to do. </p>