在通过hibernate访问数据库时,报错如下:
WARN action.RequestProcessor - Unhandled Exception thrown: class java.lang.NullPointerException
大概是什么原因呢?
[b]问题补充:[/b]
是查询的时候,调用查询方法时直接报这个错。
文件配置应该没有问题。
[b]问题补充:[/b]
这是全部的错误提示:
25609 [http-8888-1] WARN action.RequestProcessor - Unhandled Exception thrown: class java.lang.NullPointerException
2008-8-14 14:48:17 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
java.lang.NullPointerException
at com.afa.struts.action.Test1Action.execute(Test1Action.java:19)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
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:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
我的访问数据库的方法如下:
public List findAll(Class clazz) {
try {
return this.getHibernateTemplate().find(" from " + clazz.getName());
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("e "+e.getMessage());
}
return null;
}
调用该方法时参数类是通过hibernate映射而成的。
我怀疑是不是没有通过hibernate得到对象?
[b]问题补充:[/b]
这是action的execute方法:
public ActionForward execute(ActionMapping mapping, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
System.out.println("action!");
ComDaoIMPL comservice=new ComDaoIMPL();
comservice.findAll(Test.class); return mapping.findForward("test1");
}
这是ComServiceIMPL方法:
public class ComServiceIMPL implements IComService {
Comdao comdaoimpl;
public Comdao getComdaoimpl() {
return comdaoimpl;
}
public void setComdaoimpl(Comdao comdaoimpl) {
this.comdaoimpl = comdaoimpl;
}
public List findAll(Class clazz) {
return this.comdaoimpl.findAll(clazz);
}
}
其中comdaoimpl是通过spring依赖注入的:
其中comDao对应的方法是:
public List findAll(Class clazz) {
try {
return this.getHibernateTemplate().find(" from " + clazz.getName());
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("e "+e.getMessage());
}
return null;
}
不知道问题出在哪个环节上了。
[b]问题补充:[/b]
这是spring ,hibernat配置文件 spring-config.xml
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
class="com.mchange.v2.c3p0.ComboPooledDataSource">
com.microsoft.jdbc.sqlserver.SQLServerDriver
jdbc:microsoft:sqlserver://192.168.0.7:1433;DatabaseName=dsltxsalary
5
10
60
50
true
sa
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
org.hibernate.dialect.SQLServerDialect
true
classpath*:com/afa/auto/*.hbm.xml
<!--设置事务管理 -->
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 设置基本DAO对象 -->
<bean id="baseDao" class="com.afa.dao.BaseDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id = "comDAO" parent = "baseDao" class = "com.afa.dao.ComDaoIMPL">
</bean>
<bean id="comService" class="com.afa.service.ComServiceIMPL">
<property name="comdaoimpl">
<ref bean="comDAO"/>
</property>
</bean>
<bean id="basetransactionproxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="comService" />
</property>
<property name="transactionAttributes">
<props>
<prop key="delete*">PROPAGATION_REQUIRED,-InfoObjectException</prop>
<prop key="insert*">PROPAGATION_REQUIRED,-InfoObjectException</prop>
<prop key="update*">PROPAGATION_REQUIRED,-InfoObjectException</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly,-InfoObjectException</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly,-InfoObjectException</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly,-InfoObjectException</prop>
</props>
</property>
</bean>
<!-- 临时文件-->
<import resource = "base_action_config_afa.xml"/>
<import resource = "spring_action_config_afa.xml"/>
<bean id="baseAction" class="com.afa.BaseAction">
<property name="comservice">
<ref bean="basetransactionproxy"/>
</property>
</bean>
这是spring action
class="com.afa.struts.action.Test1Action">
这是struts action
我将那一句注释掉还是老毛病,快晕死了,是不是hibernate的部署有问题?
[b]问题补充:[/b]
在这里注入的:
其他action 都继承BaseAction
[b]问题补充:[/b]
折磨了我将近一周的问题就在忘记了继承baseAction的这个配置。
疯了。
十分感谢小疯子的帮助。