Hibernate实例化报java.lang.NullPointerException:
错误代码如下:
java.lang.NullPointerException
at com.fuyun.hp.hibernate.mapping.AdsModuleDAO.getInstance(AdsModuleDAO.java:14)
at org.apache.jsp.main_jsp._jspService(main_jsp.java:63)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
main.jsp:(简单测试用)
<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@page import="java.util.List"%>
<%@page import="com.fuyun.hp.hibernate.mapping.AdsModule"%>
<%@page import="com.fuyun.hp.hibernate.mapping.AdsModuleDAO"%>
<%
AdsModule topModule;
AdsModuleDAO moduleDao = AdsModuleDAO.getInstance();
List <AdsModule> topList = moduleDao.find("from AdsModule order by moduleIndex");
for (int i = 0; i < topList.size(); i++)
{
topModule = topList.get(i);
out.println(topModule.getModuleName() + ":" + topModule.getCnName());
}
%>
AdsModuleDAO.java:
package com.fuyun.hp.hibernate.mapping;
import com.fuyun.hp.common.GenericHibernateDao;
import com.fuyun.hp.common.SpringContext;
public class AdsModuleDAO extends GenericHibernateDao <AdsModule, String>
{
/**
* 获得DAO实例
* @return
*/
public static AdsModuleDAO getInstance()
{
return (AdsModuleDAO)SpringContext.getContext().getBean(AdsModuleDAO.class.getSimpleName());
}
}
applicationContext.xml:(配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/database.properties"/>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}"> </property>
<property name="url" value="${db.url}"> </property>
<property name="username" value="${db.username}"> </property>
<property name="password" value="${db.password}"> </property>
</bean>
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true"> </bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="lobHandler" ref="lobHandler"> </property>
<!-- 设置Jbpm中cfg.xml文件的映射,这里从jbpm-jpdl.jar里读取cfg.xml文件-->
<property name="mappingJarLocations">
<list>