- This topic has 14 replies, 2 voices, and was last updated 20 years, 3 months ago by
Frank.
-
AuthorPosts
-
FrankMemberHello,
I am a newbie with hibernate, and have generated a default app in ME.
When I run it in Tomcat 5, I get:
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)root cause
java.lang.NullPointerException
org.apache.jsp.index_jsp._jspService(index_jsp.java:64)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)It is dying on: hibernateSession = HibernateSessionFactory.currentSession();
Here is my code.
<%@ page language=”java” import=”java.util.*,net.sf.hibernate.*,com.stemc.*” %>
<%@ page import=”net.sf.hibernate.Session” %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
Session hibernateSession = null;
Transaction myTransaction = null;
Iterator authors = null;try {
hibernateSession = HibernateSessionFactory.currentSession();
// myTransaction = hibernateSession.beginTransaction();
// Criteria query = hibernateSession.createCriteria(Author.class);
// authors = query.list().iterator();
// myTransaction.commit();
// hibernateSession.close();
} catch (Exception e) {
out.println(e.getMessage().toString());
} finally {
try { hibernateSession.close();}
catch (Exception e2) {;}
}
%>Thanks
Frank
April 20, 2005 at 9:41 am #228385
Riyad KallaMemberFrank, what is currentSession() doing that would cause an NPE? Please paste the code of that method.
April 20, 2005 at 9:43 am #228388
FrankMemberThis was generated by ME
package com.stemc;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.cfg.Configuration;/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html}.
*/
public class HibernateSessionFactory {/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package – the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = “/hibernate.conf.xml”.
* CONFIG_FILE_LOCATION = “/com/foo/bar/myhiberstuff.conf.xml”.</code>
*/
private static String CONFIG_FILE_LOCATION = “/hibernate.cfg.xml”;/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();/** The single instance of hibernate SessionFactory */
private static net.sf.hibernate.SessionFactory sessionFactory;/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println(“%%%% Error Creating SessionFactory %%%%”);
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}return session;
}/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);if (session != null) {
session.close();
}
}/**
* Default constructor.
*/
private HibernateSessionFactory() {
}}
April 20, 2005 at 9:47 am #228391
Riyad KallaMemberDo me a favor, set a breakpoint on the first line of currentSession, and then step line by line through the method until the exception is thrown, which line was it?
April 20, 2005 at 10:07 am #228397
FrankMemberI am having trouble getting debug to work.
Failed to connect to remote VMWhat can I do?
April 20, 2005 at 10:50 am #228400
FrankMemberI get: Error reading resource: /Author.hbm.xml
Here is code:
<?xml version=”1.0″?>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD 2.0//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd” ><!– DO NOT EDIT: This is a generated file that is synchronized –>
<!– by MyEclipse Hibernate tool integration. –>
<!– Created Tue Apr 19 14:10:14 EDT 2005 –>
<hibernate-mapping package=””><class name=”Author” table=”author”>
<id name=”authorId” column=”author_id” type=”java.lang.String”>
<generator class=”uuid.hex”/>
</id><property name=”firstName” column=”first_name” type=”java.lang.String” not-null=”true” />
<property name=”lastName” column=”last_name” type=”java.lang.String” not-null=”true” />
<property name=”homePhone” column=”home_phone” type=”java.lang.String” not-null=”true” />
</class></hibernate-mapping>
April 20, 2005 at 10:56 am #228401
Riyad KallaMemberI am having trouble getting debug to work.
Failed to connect to remote VMCheck the debugging section here: http://www.myeclipseide.com/images/tutorials/quickstarts/webprojects/
You don’t need to connect to anything, it is automatic if you started the app server with MyEclipse, just set a break point and run it.
April 20, 2005 at 12:23 pm #228407
FrankMemberThanks,
Same error:
Error reading resource: /Author.hbm.xml
April 20, 2005 at 12:29 pm #228408
Riyad KallaMemberWhat does your hibernate.cfg.xml file look like, where is your Author.hbm.xml file located in your project?
April 20, 2005 at 12:32 pm #228409
FrankMember<?xml version=’1.0′ encoding=’UTF-8′?>
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD 2.0//EN”
“http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd”><!– DO NOT EDIT: This is a generated file that is synchronized –>
<!– by MyEclipse Hibernate tool integration. –>
<hibernate-configuration><session-factory>
<!– properties –>
<property name=”connection.username”>root</property>
<property name=”connection.url”>jdbc:mysql://10.x.x.x:3306/library</property>
<property name=”dialect”>net.sf.hibernate.dialect.MySQLDialect</property>
<property name=”connection.password”>password</property>
<property name=”connection.driver_class”>com.mysql.jdbc.Driver</property><!– mapping files –>
<mapping resource=”/Author.hbm.xml”/></session-factory>
</hibernate-configuration>
April 20, 2005 at 12:45 pm #228410
Riyad KallaMember1) Where is your hibernate.cfg.xml file located?
2) Where is your Author.hbm.xml file located?I believe this is a simple path issue as the path you have for Author implies that it is in the root of your project, which I doubt it is and it shouldn’t actually be. But I just want to make sure.
April 20, 2005 at 12:47 pm #228411
FrankMemberC:\tomcat\webapps\authors\WEB-INF\classes
Thanks
April 20, 2005 at 12:54 pm #228413
Riyad KallaMemberThen that means they are BOTH in the root of your /src tree of your project, if this is the case, edit the hibernate.cfg.xml file and remove the forward slash before Author.
April 20, 2005 at 12:58 pm #228414
FrankMemberThank you so much.
Frank
April 20, 2005 at 1:54 pm #228417
FrankMemberI also had to change my Author.hbm.xml
from
<class name=”Author” table=”author”>
to
<class name=”com.stemc.Author” table=”author”>Regards,
Frank
-
AuthorPosts