David
David

Reputation: 20063

Hibernate null pointer

I'm getting a null pointer on line 15...below (Session session = HibernateUtil.getSessionFactory.getCurrentSession();)

public static void main(String[] args) {
    // TODO Auto-generated method stub
    HibernateUtil.buildSessionFactory();
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    User u = new User();
    u.setEmail("[email protected]");
    u.setFirstName("David");
    u.setLastName("Gray");

    session.save(u);
    session.getTransaction().commit();
    System.out.println("Record committed");
    session.close();

My HibernateUtil is as follows...

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {

    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    public static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();     
            return new Configuration().configure().buildSessionFactory(serviceRegistry);
        } 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;
    }

}

Off topic, but if anyone also has any standard transaction hibernate utils they could recommend that would be ace. But I can't see why I'm getting the nullpointer, the hibernate.cfg.xml seems to validate fine as the logs say it could connect to the db etc.

INFO : org.hibernate.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
INFO : org.hibernate.Version - HHH000412: Hibernate Core {4.1.0.Final}
INFO : org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
INFO : org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist
INFO : org.hibernate.cfg.Configuration - HHH000043: Configuring from resource: /hibernate.cfg.xml
INFO : org.hibernate.cfg.Configuration - HHH000040: Configuration resource: /hibernate.cfg.xml
WARN : org.hibernate.internal.util.xml.DTDEntityResolver - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
INFO : org.hibernate.cfg.Configuration - HHH000041: Configured SessionFactory: null
INFO : org.hibernate.cfg.Configuration - HHH000043: Configuring from resource: /hibernate.cfg.xml
INFO : org.hibernate.cfg.Configuration - HHH000040: Configuration resource: /hibernate.cfg.xml
WARN : org.hibernate.internal.util.xml.DTDEntityResolver - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
INFO : org.hibernate.cfg.Configuration - HHH000041: Configured SessionFactory: null
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000402: Using Hibernate built-in connection pool (not for production use!)
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000115: Hibernate connection pool size: 1
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000006: Autocommit mode: false
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/assessme]
INFO : org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000046: Connection properties: {user=root, password=****}
INFO : org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
INFO : org.hibernate.engine.transaction.internal.TransactionFactoryInitiator - HHH000399: Using default transaction strategy (direct JDBC transactions)
INFO : org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory - HHH000397: Using ASTQueryTranslatorFactory
INFO : org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000228: Running hbm2ddl schema update
INFO : org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000102: Fetching database metadata
INFO : org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000396: Updating schema
INFO : org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000232: Schema update complete

Upvotes: 1

Views: 9650

Answers (3)

Akash Yadav
Akash Yadav

Reputation: 2421

You Created the instance of the session factory but missed to assign it to the class variable

so its being null , default value for reference types HibernateUtil.sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry); return HibernateUtil.sessionFactory;

instead of return new Configuration().configure().buildSessionFactory(serviceRegistry); would help.

Upvotes: 1

Mike Bockus
Mike Bockus

Reputation: 2079

It looks like you're not actually setting HibernateUtils.sessionFactory in your buildSessionFactory() method. Since sessionFactory is never set, you're returning a null object when you call HibernateUtil.getSessionFactory(). You can either set sessionFactory in your buildSessionFactory() method, or change your code to the following:

public static void main(String[] args) {
Session session = HibernateUtil.buildSessionFactory().getCurrentSession();

Upvotes: 1

xpapad
xpapad

Reputation: 4456

You never initialize sessionFactory in HibernateUtil. Perhaps buildSessionFactory should set it instead of just returning, e.g.

sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry)
return sessionFactory;

Upvotes: 1

Related Questions