Gihan
Gihan

Reputation: 63

Hibernate 5 "main" java.lang.NullPointerException

I create a small java class for hibernate but its gives an exception.I'm using Hibernate 5.0.2 and java 8 on ubuntu 14.04. My classes and Hibernate configuration files is as follows.Hope someone can help me to find a solution to this.

This is the exception that occurs.

java.lang.NullPointerException
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.getResources(ClassLoaderServiceImpl.java:173)
at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:348)
at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:393)
at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:474)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:324)
at org.hibernate.integrator.internal.IntegratorServiceImpl.<init>(IntegratorServiceImpl.java:40)
at org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:212)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:119)
at com.gim.gimpro.HibernateTest.setUp(HibernateTest.java:43)
at com.gim.gimpro.HibernateTest.main(HibernateTest.java:18)

Here is my code

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.gim.hibe.UserDetails;


public class HibernateTest {

    private SessionFactory sessionFactory;

    public static  void main(String[] args){
        HibernateTest hibernateTest=new HibernateTest();

        try {
            hibernateTest.setUp();
            hibernateTest.save();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    void save(){
        UserDetails userDetails=new UserDetails();
        userDetails.setUserId(1);
        userDetails.setUserName("Gihan");
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save( userDetails );
        session.getTransaction().commit();
        session.close();
    }


    protected void setUp() throws Exception {

        sessionFactory = new Configuration().configure() .buildSessionFactory();
    }
 }

UserDetails class

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "USER_DETAILS")
public class UserDetails {

@Id
private int userId;
private String userName;


public int getUserId() {
    return userId;
}

public void setUserId(int userId) {
    this.userId = userId;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

}

Hibernate Configuration

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 5.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<property name="hibernate.hbm2ddl.auto">create</property>
		<mapping class="com.gim.hibe.UserDetails"/>
	</session-factory>
</hibernate-configuration>

Upvotes: 0

Views: 1478

Answers (1)

shankarsh15
shankarsh15

Reputation: 1967

This Exception can occur because of below 2 reasons:

  1. Please check if package com.gim.hibe for UserDetails class is correct.

  2. Also see if Hibernate libraries are properly included in the classpath.

Upvotes: 1

Related Questions