-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Null Pointer Exception while trying to run Hibernate example
PostPosted: Fri May 17, 2013 6:23 am 
Newbie

Joined: Fri May 17, 2013 5:58 am
Posts: 1
I am a newbie and trying out a simple example posted at the following site - http://www.mastertheboss.com/quickstart-tutorials-hibernate/jpa/hibernate-tutorial-with-eclipse .

Here are the relevant versions of tools that I am using:
1. IDE - Eclipse Juno
2. Hibernate v 4.2.1
3. Apache Derby v 10.10.1.1

Here is the hibernate.cfg.xml configuration details -

Code:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <!-- hibernate dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>


        <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
        <property name="hibernate.connection.url">jdbc:derby:C:\Users\user1\MyDbTest</property>
        <property name="hibernate.connection.username"></property>
        <property name="hibernate.connection.password"></property>
        <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

        <!-- Automatic schema creation (begin) === -->
        <property name="hibernate.hbm2ddl.auto">create</property>


        <!-- Simple memory-only cache -->
        <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- ############################################ -->
        <!-- # mapping files with external dependencies # -->
        <!-- ############################################ -->

        <mapping resource="com/sample/DBAccess/Employee.hbm.xml" />

    </session-factory>
</hibernate-configuration>


The error is as listed below:
    May 17, 2013 3:25:17 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
    INFO: HHH000046: Connection properties: {user=, password=****}
    Initial SessionFactory creation failed.java.lang.NullPointerException
    Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.sample.DBAccess.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:21)
    at com.sample.DBAccess.TestDBAccess.main(TestDBAccess.java:12)
    Caused by: java.lang.NullPointerException
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:214)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:242)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:117)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:78)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2293)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2289)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1758)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1798)
    at com.sample.DBAccess.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:16)

This is the code:

Listing-1
Code:
package com.sample.DBAccess;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

@SuppressWarnings("deprecation")
public class SessionFactoryUtil {
   private static final SessionFactory sessionFactory;
       static {
           try {
               // Create the SessionFactory from hibernate.cfg.xml
               //sessionFactory = new Configuration().configure().buildSessionFactory();
              Configuration cfg = new Configuration();
              cfg.configure("hibernate.cfg.xml");
              sessionFactory = cfg.buildSessionFactory();
              
           } catch (Throwable ex) {
               // Make sure you log the exception, as it might be swallowed
               System.err.println("Initial SessionFactory creation failed." + ex);
               throw new ExceptionInInitializerError(ex);
           }
       }

       public static SessionFactory getSessionFactory() {
           return sessionFactory;
       }

   }


Listing-2
Code:
package com.sample.DBAccess;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;


public class TestDBAccess {
   public static void main(String[] args) {
      Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        createEmployee(session);
        queryEmployee(session);
    }

    private static void queryEmployee(Session session) {
        Query query = session.createQuery("from Employee");                 
        List <Employee>list = query.list();
       java.util.Iterator<Employee> iter = list.iterator();
        while (iter.hasNext()) {
           Employee employee = iter.next();
            System.out.println("Employee: \"" + employee.getEmpNo() +"\", " + employee.getEmpName() +"\", " + employee.getAddr() + "\", " + employee.getDesignation() + "\", " + employee.getDept());
        }
        session.getTransaction().commit();
    }

    public static void createEmployee(Session session) {
           Employee employee = new Employee();
           employee.setEmpNo(4943);
           employee.setEmpName("Next Employee");       
           employee.setAddr("D3-104, South City, Some City");       
           employee.setDesignation("Manager");
           employee.setDept(102);
           session.save(employee);
   }
}


Can someone please help me figure out where is the issue?

Thanks in advance,

Ramas.


Top
 Profile  
 
 Post subject: Re: Null Pointer Exception while trying to run Hibernate example
PostPosted: Tue May 21, 2013 6:39 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
INFO: HHH000046: Connection properties: {user=, password=****}

=> There is no username in your config. Maybe that's the problem, maybe not...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.