-->
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: i am getting error while running simple hibernate program
PostPosted: Wed May 10, 2006 8:16 am 
Newbie

Joined: Wed May 10, 2006 8:12 am
Posts: 9
ERROR as

alert : "fatal exception occured, program will exit"

and error msg :

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

my progrm is :

package hibernate.net.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");
Emp emp = new Emp();
emp.setEmpNo(7999);
emp.setENAME("VIswanath");
emp.setJOB("CLERK");
emp.setMGR(7902);
emp.setHIREDATE(null);
emp.setSAL(22000.00);
emp.setCOMM(0.00);
emp.setDEPTNO(20);

session.save(emp);
System.out.println("Done");

}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush(); //GETTING ERROR HERE
session.close();
}
}
}

Please suggest to resolve

Thanks in advance
-KRVR


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 8:54 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Your session object is NULL.

Didnt you have any EXCEPTION message from the line
System.out.println(e.getMessage());

Also print trace with e.printStackTrace(); which will give you more information.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 10:08 am 
Newbie

Joined: Wed May 10, 2006 8:12 am
Posts: 9
[quote="bkmr_77"]Your session object is NULL.

Didnt you have any EXCEPTION message from the line
[b]System.out.println(e.getMessage()); [/b]

Also print trace with [b]e.printStackTrace();[/b] which will give you more information.[/quote]


Thanks for your reply.

But i am getting the session as

session =sessionFactory.openSession();

do you say that its getting the null instead of valid session
i have the hibernate.cfg.xml as

<?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">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.68.44:1521:EXAMPLE</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</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>
<!-- Mapping files -->
<mapping resource="emp.hbm.xml"/>
</session-factory>
</hibernate-configuration>

is it invalid ?
please suggest.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 10:15 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I cant say if your configuration file is invalid, atleast in theory it looks good. If you are using oracle10g as db then better use another dialect as below:

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


I just put extra if condition on session == null below to see if your session is really NULL. Also I moved session.flush() from finally into try block as below.

Code:
try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
if ( session == null ) {
   System.out.println( "MY SESSION IS RETURNING NULL" );
}

//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Emp emp = new Emp();
emp.setEmpNo(7999);
emp.setENAME("VIswanath");
emp.setJOB("CLERK");
emp.setMGR(7902);
emp.setHIREDATE(null);
emp.setSAL(22000.00);
emp.setCOMM(0.00);
emp.setDEPTNO(20);

session.save(emp);
System.out.println("Done");

System.out.println( "BEFORE SESSION.FLUSH....." );
// Actual contact insertion will happen at this step
session.flush(); //GETTING ERROR HERE

}catch(Exception e){
System.out.println(e.getMessage());
}finally{
session.close();
}
}
}


Also if you can provide stack trace, it will give information as to why you are getting such problem. Its a better idea to push


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 10:27 am 
Newbie

Joined: Wed May 10, 2006 8:12 am
Posts: 9
Hi,
finally i have traced the error as you said, i got the folowing error.

java.lang.NullPointerException
null
at hibernate.net.roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:45)
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:111)
at hibernate.net.roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:17)
Exception in thread "main"

so getting at the following line

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

so do application is finding my configuration file or not?

Thanks for your followup


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 10:32 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Quote:
SessionFactory sessionFactory =new Configuration().configure().buildSessionFactory();

so do application is finding my configuration file or not?


With these 2 lines of information and above posts information I cant comment on anything unless you provide required information( atleast error stack trace )

[PS: After seeing the error I thought you added required commons-logging.jar file in your classpath. If not, please do it as calin pointed out in the below post. Secondly, never hide any exception with simple println statements instead you can print complete trace as it will give more information and save a lot of time]


Last edited by bkmr_77 on Wed May 10, 2006 10:53 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 10:49 am 
Newbie

Joined: Wed Nov 23, 2005 5:01 am
Posts: 15
Hi,
From your last post:
Quote:
java.lang.NullPointerException
null
at hibernate.net.roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:45)
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:111)
at hibernate.net.roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:17)
Exception in thread "main"


you can see that the program is looking for LogFactory class from commons-logging.jar library.
So I guess that you don't have the library in your classpath. The library can be found into your hibernate distribution into the lib directory.

You have to put in the program classpath all required libraries from the hibernate/lib directory.

I hope this help.

Calin.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 11:43 am 
Beginner
Beginner

Joined: Sun Feb 19, 2006 3:50 am
Posts: 34
After you get the logging set up, it may still fail because you're not using a transaction. Try surrounding the "talking to the database" bits with a transaction. Before session.save(emp), put session.beginTransaction(). After session.flush(), put session.getTransaction().commit().


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.