新手提问:尝试第一个hibernate遇到的问题 http state 500
几天前才开始学Java,MyEclipse+struts+hibernate+mysql 的构架刚刚把struts调好,这次又遇到了hibernate的问题。还请懂hibernate的大侠为我指出问题。
按照一个教程搭的构架,运行时候有数据库操作的时候就会出错。如下
HTTP Status 500 -
--------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
com.Hibernate.SessionFactory.currentSession(SessionFactory.java:56)
com.yourcompany.struts.form.AddAdminForm.validate(AddAdminForm.java:52)
org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:942)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:255)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.12 logs.
--------------------------------------------
Apache Tomcat/5.5.12
我的SessionFactory是MyEclipse自动生成的,期中的代码基本没改。
package com.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.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 SessionFactory {
/**
* 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 org.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 SessionFactory() {
}
}
[解决办法]
NullPointerException 空指针异常
看看tomcat的日志 里面有明确的错误信息
[解决办法]
JDBC Driver class not found: com.mysql.jdbc.Driver
驱动装载错误,仔细看看连接JDBC那一部分~~
[解决办法]
你没有加载MySQL驱动包
系统找不到com.mysql.jdbc.Driver
所以没有成功生成sessionFactory所以你的第56行出现了空指针异常
[解决办法]
你自己看下你在myeclipse中连接数据库时用的包是否导入到项目中来了
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html