2

I am getting null pointer exception at the line

SessionFactory sesionFactory = new Configuration().configure().buildSessionFactory() ;

any suggestion what might be causing it ??

error log says this :

Exception in thread "main" 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.Configuration.buildTypeRegistrations(Configuration.java:1797)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1755)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
    at com.hussi.model.Main.main(Main.java:15)

my Main class File :

package com.hussi.model;

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

public class Main {
    public static void main(String[] args) 
    {
        User user = new User();
        user.setUsername("hussi");
        user.setPassword("maria");

        SessionFactory sesionFactory = new Configuration().configure().buildSessionFactory() ;
        Session session = sesionFactory.openSession();
        Transaction tr = session.beginTransaction();
        session.save(user);
        session.flush();
        session.close();
    }

}

my model file

package com.hussi.model;

public class User 
{
    int user_id;
    String username;
    String password;

    public int getUser_id() {
        return user_id;
    }
    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }


    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }


    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String toString()
    {
        return "username==>"+this.username+" : password==>"+this.password;
    }

}

my user.hbm.xml file

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.hussi.model.User" table="users">
         <id name="user_id" type="int" column="user_id">
            <generator class="increment" />
        </id>

        <property name="username">
            <column name="username"/>
        </property>

        <property name="password">
            <column name="password"/>
        </property>
    </class>
</hibernate-mapping>

my hibernate configuration file : hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc.mysql://localhost:3306/my_hibernate_1</property>
        <property name="connection.username">root</property>
        <property name="connecttion.password">root</property>

        <!-- Database connection settings -->
        <property name="connection.pool_size">1</property>

        <!-- MySql Dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">false</property>

        <mapping resource="user.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
4
  • Can you share the full exception trace? Hibernate generally tells you why it is not able to create the session factory. Commented Jun 30, 2013 at 1:15
  • Probably a problem with your connection string. See similar problems: stackoverflow.com/questions/16596528/… and stackoverflow.com/questions/16872456/… Commented Jun 30, 2013 at 1:31
  • 2
    Have you tried changing jdbc.mysql: to jdbc:mysql: in your connection.url? Commented Jun 30, 2013 at 1:34
  • What version? Could you please check if this happens with latest HBN version and if so, create an issue to handle the NPE? Thanks. Commented Jun 30, 2013 at 2:41

2 Answers 2

8

You have given wrong url

below is correct one

jdbc:mysql://localhost:3306/my_hibernate_1

Just replaced jdbc.mysql with jdbc:mysql

Sign up to request clarification or add additional context in comments.

Comments

1

Try changing the hibernate config connection.url property to jdbc:myql

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.