Solved

Exception in thread "main" java.lang.NullPointerException at dao.EmployeeDao.main(EmployeeDao.java:42)

Posted on 2009-07-08
11
1,399 Views
Last Modified: 2013-11-24
Hi,
            I am new to Hibernate3.0 as well this forum.. I am happy to be member this forum...

Right now i am learning Hibernate ..herewith i have simple written simple application
for storing the data into DB using hibernate.. while running the application i ama getting  above error..  see the code below.
1) This is My bean class

 

package Bean;

 

public class EmployeeBean 

{

	private String firstName;

	private String lastName;

	private String id;

	

	public String getFirstName()

	{

		return firstName;

	}

	

	public void setFirstName(String firstName)

	{

		this.firstName = firstName;

	}

	

	public String getLastName()

	{

		return lastName;

	}

	

	public void setLastName(String lastName)

	{

		this.lastName = lastName;

	}

	

	public String getId()

	{

		return id;

	}

	

	public void setId(String id)

	{

		this.id = id;

	}

 

}

 

 

2) I have created Table called Employee  using oracle with filed names Id,FirstName, LastName
 
 

3) this is mY DAo class for storing the inot database

 

 

package dao;

 

 

import org.hibernate.SessionFactory;

import org.hibernate.Session;

import org.hibernate.cfg.Configuration;

import Bean.EmployeeBean;

 

 

public class EmployeeDao 

{

	public static void main(String[] args)

	{

		Session sesssion = null;

		

		try

		  {

			 

			// Configuration configuration = new  Configuration();

			

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

		

			 sesssion = sessionFactory.openSession();

			

			 System.out.println("Inserting   Record ");

			 EmployeeBean employeeBean = new  EmployeeBean();

			 employeeBean.setId("350");

			 employeeBean.setFirstName("Mahesh");

			 employeeBean.setLastName("Kuppusamy");

			 sesssion.save(employeeBean);

		  }

		

		catch (Exception e) 

		 {

			    

				System.out.println(e.getMessage());

		 }

		finally

		{

		  // Actual insert will happen here

			

			sesssion.flush();

			sesssion.close(); 

		}

		

		

	}

 

}

 

 

4) see the EmployeeAdd.hbm.xml here

 

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated Jun 4, 2008 2:00:58 AM by Hibernate Tools 3.2.1.GA -->

<hibernate-mapping default-lazy="false">

    <class name="Bean.EmployeeBean" table="Employee"  select-before-update="true">

    

        <id name="id" type="string">

            <column name="Id" length="3" />

            <generator class="assigned" />

        </id>

        

        <property name="firstName" type="string">

            <column name="FirstName" length="5" not-null="true"/>

  		</property>                 

  		

        <property name="lastName" type="string">

        	<column name="LastName" length="5" not-null="true" />

        </property>

               

        </class>

</hibernate-mapping>

 

 

5) see the hibernate.cfg.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

	<session-factory>

		<!-- =============================================================== -->

		<!--                        SQL SERVER DIALECT                       -->

		<!-- =============================================================== -->

	

		<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

		<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>

		<property name="hibernate.connection.username">system</property>

        <property name="hibernate.connection.password">tspl</property>

        <property name="hibernate.connection.pool_size">10</property>

        <property name="show_sql">true</property>

		<property name="dialect">org.hibernate.dialect.OracleDialect</property>

		<property name="hibernate.hbm2ddl.auto">update</property>

 

 

		<!-- =============================================================== -->

		<!--                      HIBERNATE MAPPINGS                         -->

		<!-- =============================================================== -->

		

		<mapping resource="xbm/EmployeeAdd.hbm.xml"/>

		

	 	</session-factory>

</hibernate-configuration>

Open in new window

0
Comment
Question by:maheshvrk3
  • 6
  • 3
11 Comments
 
LVL 13

Expert Comment

by:Bart_Cr
ID: 24810991
If I'm counting lines correctly, it's the "sesssion.flush()" which throws the NullPointer. This means your session did not get initialized. I think there's another exception thrown before.

Change the catch to:

}
catch (Exception e) {
    e.printStackTrace();
}
0
 
LVL 20

Expert Comment

by:a_b
ID: 24811006
Can you post the full stack trace?
0
 

Author Comment

by:maheshvrk3
ID: 24811135
I  have changed the catch statement to

catch (Exception e)
{
 e.printStackTrace();

 }

 Still i am getting the same Error

Exception in thread "main" java.lang.NullPointerException
      at dao.EmployeeDao.main(EmployeeDao.java:43)
0
 

Author Comment

by:maheshvrk3
ID: 24811185
Herewith i ahve attached the sanpshot
0
 
LVL 20

Expert Comment

by:a_b
ID: 24811195
Cannot see the snapshot.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:maheshvrk3
ID: 24811220
Herewith i ahve attached the sanpshot


stackTrace.doc
0
 
LVL 20

Expert Comment

by:a_b
ID: 24876119
Please share the solution with us.
0
 

Accepted Solution

by:
maheshvrk3 earned 0 total points
ID: 24877008


add the the conditon in the finaly

finally(
                         {
          if(session==null)
              sesssion.flush();
         sesssion.close();
}
      }

            
0
 

Author Comment

by:maheshvrk3
ID: 24878791
Solution to this issues is

add the following  conditon in the finally

finally(
                         {
          if(session==null)
              sesssion.flush();
         sesssion.close();
}
      }

           
0
 

Author Closing Comment

by:maheshvrk3
ID: 31601025
As i  have added the solution for this issue.. proceedong to close the question...

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
either24  challenge 19 84
Receive file in Servlet 1 36
countPairs challenge 7 58
wordlen challenge 3 46
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now