Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate:Nullpointer exception

 
Bk Jacky
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,

Vowels cost $500.

Have you never seen Wheel of Fortune?

-Cameron McKenzie
 
labi laba
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mark,
i have created a new thread here
http://www.coderanch.com/t/218498/ORM/java/Hibernate-configuration

any ideas/suggestions?thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic