I have a Struts and Hibernate based application. After uploading application on the server, when I login I get "HTTP Status 500 - Internal Server Error". The Exception is "javax.servlet.ServletException" and the root cause of the exception is: "java.lang.NullPointerException" at line:
tx = session.beginTransaction();
Here are few lines of code:
Code:
Transaction tx = null;
try {
String queryString =" select u from User as u where u.userName='"+username+"' and u.password='"+password+"' ";
session = HibernateSessionFactory.currentSession();
// the line where I get the NullPointerException
tx = session.beginTransaction();
Query query = session.createQuery(queryString);
...........
...........
Code:
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
ResourceBundle resourceBndl = ResourceBundle.getBundle("resources.ApplicationResources", Locale.getDefault());
try {
if (session != null && !session.isOpen())
session = null;
if (session == null) {
if (sessionFactory == null) {
//sessionFactory = new Configuration().configure().buildSessionFactory();
sessionFactory = new Configuration().configure(new File(resourceBndl.getString("hibernate.config"))).buildSessionFactory();
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
} catch (Exception e){
System.err.println("Error Creating SessionFactory");
e.printStackTrace();
}
return session;
}
resourceBndl.getString("hibernate.config"); is the path where "hibernate.cfg.xml" file is located. I got the above exception on MY COMPUTER only when path for "hibernate.cfg.xml" file was not correct. After correcting the path everything was OK. I have also corrected the path on the server (now it is server path, NOT MY COMPUTER's path), but I am still getting the same problem even after restarting the Tomcat.
Can someone please help me to solve this issue. Its very urgent.
Thanks.
Joseph Bashir