aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate:Nullpointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate:Nullpointer exception " Watch "Hibernate:Nullpointer exception " New topic
Author

Hibernate:Nullpointer exception

Bk Jacky
Ranch Hand

Joined: Jun 11, 2005
Posts: 74
HI,
I am new for hibernate, while executing main method I am getting nullpointer exception. please some body guide me whats the wrong in my code.

package roseindia.tutorial.hibernate;

import java.io.File;

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


/**
* @author Deepak Kumar
*
* http://www.roseindia.net
* Hibernate example to inset data into Contact table
*/
public class FirstExample {
public static void main(String[] args) {
Session session = null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
System.out.println("getEventInfo");
File file = new File("D:\\bea\\user_projects\\w4WP_workspaces\\Untitled\\Hyber\\hibernate.cfg.xml"); //yu see I have used full path
// This is my first run I will chenge to relative path later
System.out.println(file.exists());

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

if(sessionFactory == null) {
System.out.println("NULL"); //Testing it
}
else {
System.out.println("NOT NULL");
}

//SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
System.out.println("Sessiong Factory After");
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.save(contact);
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();

}

}

}

Error is

getEventInfo
true
Exception in thread "Main Thread" java.lang.NullPointerException
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:52)


SCJP1.4<br />SCWCD1.5<br />"Nothing is impossible"
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Break this into separate method calls:


Put your println after each one and see which line is kicking out null. Maybe even a try catch.

Here's some code from a tutorial of mine that shows you how to get started with Hiberante and Hibernate Annotations:



A Simple Tutorial on How to Add Database Records with Hibernate and JPA Annotations

Notice how I use 3 lines of code to get the the SessionFactory. That's probably a good way to start off.

Keep us appraised of your progress.

-Cameron McKenzie
Bk Jacky
Ranch Hand

Joined: Jun 11, 2005
Posts: 74
Hi Cameron,

I break down the code as per your suggestion and found out that following line is not excuting...










This line is throwing NULLPointer exception. hibernate.cf.xml and contct.hbm.xml files are in class path... i don't know what's going wrong...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

"hibernate.cf.xml"

Where's the "g"? Is it missing form your keyboard?

as in

hibernate.cfg.xml

And do you not like vowels?

"contct.hbm.xml"

is that supposed to be "contact"

Anyway, can you also post the exact stack trace that you are getting, it will show us a lot more.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Actually also looking at your code I see you have this



If you have your hibernate.cfg.xml in the root of your classpath, then you do not need to create a File object and use that method version to create your SessionFactory.

You can simple do

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

Here is the most common way people do. The make a HibernateUtil class like



I stole that completely from the hibernate.org documentation page.

Mark
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Mark,

Vowels cost $500.

Have you never seen Wheel of Fortune?

-Cameron McKenzie
labi laba
Greenhorn

Joined: Apr 11, 2008
Posts: 9
I am also having the same problem, null pointer exception. Please help me out. I have hibernate.cfg.xml in /src directory so that its automatically included to build path.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Originally posted by labi laba:
I am also having the same problem, null pointer exception. Please help me out. I have hibernate.cfg.xml in /src directory so that its automatically included to build path.


Can you create a new thread for your issue.

Include your pathing, classpath, and your hibernate.cfg.xml and how you package it all up

Thanks

Mark
labi laba
Greenhorn

Joined: Apr 11, 2008
Posts: 9
hi mark,
i have created a new thread here
http://www.coderanch.com/t/218498/ORM/java/Hibernate-configuration

any ideas/suggestions?thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate:Nullpointer exception