hi

i want to use hibernate in java+postgresql

but when i run it receive NullPointerException

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

public class FirstExample {
	public static void main(String[] args) {
		Session session = null;

		try{
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			 session =sessionFactory.openSession();
			 
			 Transaction tx = session.beginTransaction();
			 	System.out.println("Inserting Record");
				Contact contact = new Contact();
				contact.setId(10);
				contact.setFirstName("kibo");
				contact.setLastName("omid");
				session.update(contact);
				
				tx.commit();
				Book book = new Book();
				book.setStrBookName("Test");
				session.save(book);
				tx.commit();
				
				System.out.println("Done");
		}catch(Exception e){
			System.out.println(e.getMessage());
			e.printStackTrace();
		}finally{
			session.flush();
			session.close();
			System.out.println("Done");
			}
		
	}
}
public class Contact {
	private String firstName;
	private String lastName;
	private String email;
	private long id;

	/**
	 * @return Email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @return First Name
	 */
	public String getFirstName() {
		return firstName;
	}

	/** 
	 * @return Last name
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * @param string Sets the Email
	 */
	public void setEmail(String string) {
		email = string;
	}

	/**
	 * @param string Sets the First Name
	 */
	public void setFirstName(String string) {
		firstName = string;
	}

	/**
	 * @param string sets the Last Name
	 */
	public void setLastName(String string) {
		lastName = string;
	}

	/**
	 * @return ID Returns ID
	 */
	public long getId() {
		return id;
	}

	/**
	 * @param l Sets the ID
	 */
	public void setId(long l) {
		id = l;
	}

}
public class Book {
	private long lngBookId;
	private String strBookName;
	
	/**
	 * @return Returns the lngBookId.
	 */
	public long getLngBookId() {
		return lngBookId;
	}
	/**
	 * @param lngBookId The lngBookId to set.
	 */
	public void setLngBookId(long lngBookId) {
		this.lngBookId = lngBookId;
	}
	/**
	 * @return Returns the strBookName.
	 */
	public String getStrBookName() {
		return strBookName;
	}
	/**
	 * @param strBookName The strBookName to set.
	 */
	public void setStrBookName(String strBookName) {
		this.strBookName = strBookName;
	}
}
<?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="roseindia.tutorial.hibernate.Contact" table="CONTACT">
	   <id name="id" type="long" column="ID" >
	   <generator class="assigned"/>
	  </id>

	  <property name="firstName">
		 <column name="FIRSTNAME" />
	  </property>
	  <property name="lastName">
		<column name="LASTNAME"/>
	  </property>
	  <property name="email">
		<column name="EMAIL"/>
	  </property>
   </class>

   <class name="roseindia.tutorial.hibernate.Book" table="book">
		<id name="lngBookId" type="long" column="id" >
			<generator class="increment"/>
		</id>

		<property name="strBookName">
			<column name="bookname" />
		</property>
	</class>
 
   <class name="roseindia.tutorial.hibernate.Insurance" table="insurance">
		<id name="lngInsuranceId" type="long" column="ID" >
			<generator class="increment"/>
		</id>

		<property name="insuranceName">
			<column name="insurance_name" />
		</property>
		<property name="investementAmount">
			<column name="invested_amount" />
		</property>
		<property name="investementDate">
			<column name="investement_date" />
		</property>
	</class>



</hibernate-mapping>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
	<property name="hibernate.connection.driver_class">
		org.postgresql.Driver
	</property>
	<property name="hibernate.connection.url">
		jdbc:postgresql://127.0.0.1:5432/postgres
	</property>
	<property name="hibernate.connection.username">postgres</property>
	<property name="hibernate.connection.password">mypass</property>
	<property name="hibernate.connection.pool_size">10</property>
	<property name="show_sql">true</property>
	<property name="hibernate.hbm2ddl.auto">update</property>
	<!-- Mapping files -->
	<property name="myeclipse.connection.profile">
		org.postgresql.Driver
	</property>
	<property name="connection.url">
		jdbc:postgresql://127.0.0.1:5432/postgres
	</property>
	<property name="connection.username">postgres</property>
	<property name="connection.password">1</property>
	<property name="connection.driver_class">
		org.postgresql.Driver
	</property>
	<property name="dialect">
		org.hibernate.dialect.PostgreSQLDialect
	</property>
	<mapping resource="contant.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>

You don't only get a NullPointerException. You also get a stack trace. Have you tried reading it? It tells you where the error is and what is the problem. You can try posting it, since it is unlikely someone will read AND compile your entire code in order to find the error.

error is:
Exception in thread "main" java.lang.NullPointerException
at FirstExample.main(FirstExample.java:35)

error is:
Exception in thread "main" java.lang.NullPointerException
at FirstExample.main(FirstExample.java:35)

Inside the main method of the FirstExample.java file at line 35 you are using something which is null. Do you get any other errors?

no

Inside the main method of the FirstExample.java file at line 35 you are using something which is null. Do you get any other errors?

no
but if I run it without finally
means

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.postgresql.Driver;
import org.hibernate.dialect.PostgreSQLDialect;

public class FirstExample {
	public static void main(String[] args) {
		Session session = null;

		try{
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			 session =sessionFactory.openSession();
			 
			 Transaction tx = session.beginTransaction();
			 	System.out.println("Inserting Record");
				Contact contact = new Contact();
				contact.setId(10);
				contact.setFirstName("kibo");
				contact.setLastName("omid");
				session.update(contact);
				
				tx.commit();
				Book book = new Book();
				book.setStrBookName("Test");
				session.save(book);
				tx.commit();
				
				System.out.println("Done");
		}catch(Exception e){
			System.out.println(e.getMessage());
			e.printStackTrace();
		}/*finally{
			session.flush();
			session.close();
			System.out.println("Done");
			}*/
		
	}
}

receive:

Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at FirstExample.main(FirstExample.java:13)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.