-->
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.  [ 2 posts ] 
Author Message
 Post subject: NullPointerException while sessionFactory.openSession()
PostPosted: Mon Feb 11, 2008 7:50 am 
Newbie

Joined: Sat Dec 01, 2007 1:24 am
Posts: 7
Location: Pune
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

I m using Hibernate2.0
and Oracle 9i
I m getting this error,
java.lang.NullPointerException
at com.gltv3.common.HibernateUtil.getSession(HibernateUtil.java:46)
at
com.gltv3.DAO.RetriveApplMessageDAO.getMessage(RetriveApplMessageDAO.java:30)
at
com.gltv3.framework.RetriveApplMessages.getMessage(RetriveApplMessages.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:276)
at
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:196)


Code for HibernateUtil Class (method getSession)
public class HibernateUtil {
private static SessionFactory sessionFactory;

static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
}
catch (HibernateException hne) {
hne.getStackTrace();
}
}

/*
* As a Hibernate Session is non-threadsafe, ThreadLocal is used
* to hold the session for the current executing thread.
*/
public static Map sessionMap = new HashMap();
//public static final Map sessionMap =
Collections.synchronizedMap(new HashMap());
/* (non-Javadoc)
*
*/
public static Session getSession(String sessionId) throws
HibernateException{

Session s = (Session) sessionMap.get(sessionId);
try {
if (s == null || !s.isOpen() || !s.isConnected()) {
s = sessionFactory.openSession();
sessionMap.put(sessionId,s);
System.out.println("Opening New Hibernate session......");
}
}
catch (HibernateException hne) {
hne.getStackTrace();
}

return s;
}
}


Code for hibernate.cfg.xml ,
<?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>

<!-- Local connection properties -->

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

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

<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
<!--org.apache.commons.dbcp.BasicDataSourceFactory-->
</property>

<property name="hibernate.connection.username">exldemo</property>
<property name="hibernate.connection.password">exldemo</property>
<property name="hibernate.connection.pool_size">25</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_size">50</property>
<property name="hibernate.c3p0.timeout">200</property>
<property name="hibernate.c3p0.max_statements">10000</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property
name="hibernate.cglib.use_reflection_optimizer">true</property>
<property
name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
<property name="hibernate.show_sql">true</property>


<mapping resource="/com/gltv3/hibernate/ApplMessage.hbm.xml"/>

</session-factory>
</hibernate-configuration>

And I have hibernate2.jar file in my class path :
Please help me to solve this problem.

Thanks & Regards,
Gazy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 11, 2008 8:26 am 
Newbie

Joined: Mon Feb 11, 2008 5:46 am
Posts: 5
Most likely you have problem with hibernateSession initialization. Your exception handling is bad and block
Code:
catch (HibernateException hne) {
hne.getStackTrace();
}

does not make any sense.

Rewrite your static initializer like this:
Code:
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
}
catch (HibernateException hne) {
throw new ExceptionInInitializerError(hne);
}
}

and if sessionFactory is not created (I guess this is the most likely case) you will see why it fails.

On a side note your getSession() method just eats up HibernateException without even logging exception which is poor way to code.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.