-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Java Null Pointer Exception
PostPosted: Thu Jul 19, 2007 12:18 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
Hi all
I am getting exception in the following program when i try to execute.

Code:

package roseindia.tutorial.hibernate;

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



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
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
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(6);
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
System.out.println("success");
session.beginTransaction().commit();
session.flush();
session.close();

}

}
}


Exception:

success
Exception in thread "main" java.lang.NullPointerException
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:37)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 1:17 am 
Newbie

Joined: Fri Jun 29, 2007 9:03 am
Posts: 12
Hi Shiv,

Can u pls point out which is line 37. Also transaction should be started before session.save() is done . And close after it only.
Also please paste the logs or stacktrace .
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 1:49 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
rastogha wrote:
Hi Shiv,

Can u pls point out which is line 37. Also transaction should be started before session.save() is done . And close after it only.
Also please paste the logs or stacktrace .
Thanks


Line 37 is after the method close();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 1:54 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
shiva_krish wrote:
rastogha wrote:
Hi Shiv,

Can u pls point out which is line 37. Also transaction should be started before session.save() is done . And close after it only.
Also please paste the logs or stacktrace .
Thanks


Line 37 is after the method close();

Sorry its the 37th line is the close() method.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 2:03 am 
Beginner
Beginner

Joined: Wed Jul 18, 2007 7:38 am
Posts: 23
shiva_krish wrote:
shiva_krish wrote:
rastogha wrote:
Hi Shiv,

Can u pls point out which is line 37. Also transaction should be started before session.save() is done . And close after it only.
Also please paste the logs or stacktrace .
Thanks


Line 37 is after the method close();

Sorry its the 37th line is the close() method.


When i debug the it shows on flush() method.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 3:18 am 
Newbie

Joined: Fri Jun 29, 2007 9:03 am
Posts: 12
not sure but check in debug mode
session or session.beginTransaction() is null .....


Top
 Profile  
 
 Post subject: transaction error
PostPosted: Fri Jul 20, 2007 12:30 pm 
Newbie

Joined: Fri Dec 15, 2006 11:09 am
Posts: 10
You begin and commit your transaction in a single line, with no work being accomplished. Your session.save(object) should be inside the transaction, and it is not, in your code shown.

Typical to do it in these three steps:
a. begin your transaction
b. do your work - in this case your session.save(yourObject)
c. either commit transaction if successful or rollback transaction if unsuccessful.

Hope this helps,
Jeff


Top
 Profile  
 
 Post subject: transaction and flush
PostPosted: Fri Jul 20, 2007 12:50 pm 
Newbie

Joined: Fri Dec 15, 2006 11:09 am
Posts: 10
also, when you commit the transaction, it will flush. You can remove the session.flush() you have after the commit.

Something like this:
(note rollback not used for this simple example)

Transaction tx = session.beginTransaction();
// do your persistence work - any saves, deletes, updates
session.save(contact);
tx.commit();
session.close();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.