问题描述
在通过hibernate访问数据库时,报错如下:WARN action.RequestProcessor - Unhandled Exception thrown: class java.lang.NullPointerException大概是什么原因呢?问题补充:是查询的时候,调用查询方法时直接报这个错。文件配置应该没有问题。问题补充:这是全部的错误提示:25609 [http-8888-1] WARN action.RequestProcessor - Unhandled Exception thrown: class java.lang.NullPointerException2008-8-14 14:48:17 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet.service() for servlet action threw exceptionjava.lang.NullPointerExceptionat 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 blocke.printStackTrace();System.out.println("e "+e.getMessage());}return null;}调用该方法时参数类是通过hibernate映射而成的。我怀疑是不是没有通过hibernate得到对象?问题补充:这是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依赖注入的:<bean id="comService" class="com.afa.service.ComServiceIMPL"><property name="comdaoimpl"><ref bean="comDAO"/></property></bean>其中comDao对应的方法是:public List findAll(Class clazz) {try {return this.getHibernateTemplate().find(" from " + clazz.getName());} catch (DataAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("e "+e.getMessage());}return null;}不知道问题出在哪个环节上了。问题补充:这是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"><beans><bean id="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass"><value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value></property><property name="jdbcUrl"><value>jdbc:microsoft:sqlserver://192.168.0.7:1433;DatabaseName=dsltxsalary</value></property><property name="properties"> <props> <prop key="c3p0.minPoolSize">5</prop> <prop key="hc3p0.maxPoolSize">10</prop> <prop key="hc3p0.timeout">60</prop> <prop key="c3p0.max_statement">50</prop> <prop key="c3p0.testConnectionOnCheckout">true</prop> <prop key="user">sa</prop> <prop key="password"></prop> </props> </property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource"><ref bean="dataSource" /></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop><prop key="hibernate.show_sql">true</prop></props></property><property name="mappingLocations"><list><value>classpath*:com/afa/auto/*.hbm.xml</value></list></property></bean><!--设置事务管理 --><bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory"> <ref bean="sessionFactory" /> </property></bean><!-- 设置基本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></beans>这是spring action<beans><bean name="/test1" singleton="false"class="com.afa.struts.action.Test1Action"></bean></beans>这是struts action<action path="/test1" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="test1" path="/hello.jsp" /> </action>我将那一句注释掉还是老毛病,快晕死了,是不是hibernate的部署有问题?问题补充:在这里注入的:<bean id="baseAction" class="com.afa.BaseAction"> <property name="comservice"> <ref bean="basetransactionproxy"/> </property> </bean> 其他action 都继承BaseAction问题补充:折磨了我将近一周的问题就在忘记了继承baseAction的这个配置。疯了。十分感谢小疯子的帮助。
解决方案
你用的是这个Action吗:<bean name="/test1" singleton="false"class="com.afa.struts.action.Test1Action"></bean> 怎么没有看到comservice注入?如果你是想继承baseAction, 也没有看到你继承的配置啊?
解决方案二:
......private ComDaoIMPL comservice ;public void setComservice(comservice ){this.comservice = comservice ;}......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");}
解决方案三:
ComDaoIMPL comservice=new ComDaoIMPL();不应该是new的, 这样的话comservice中的comdaoimpl没有初始化!应该直接用spring注入进来:引用......private ComDaoIMPL comservice ;public void setComservice(comservice ){ this.comservice = comservice ;}......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");} 你的action交给spring管理, 然后注入comService!
解决方案四:
楼主能不能把Test1Action的execut的方法贴出来, 主要看下19行是怎么写的at com.afa.struts.action.Test1Action.execute(Test1Action.java:19)
解决方案五:
"WARN action.RequestProcessor - Unhandled Exception thrown: class java.lang.NullPointerException "昨天刚调试一个这样的错误。问题出在当一个对象的实例 object为NULL时,调用该object的getXXX()或者setXXX()时,就报这个错误。
解决方案六:
建议将的查询的那段代码贴出来, 看是不是hql, 或者是传递参数有问题.完整的异常信息最好也贴上来!
解决方案七:
空指针异常 具体是哪里那你得把完整的报错信息发上来, 起码得以你发的这句的为中心 上下5行内的发过来吧
解决方案八:
这个提示太少了,建议自己debug一下.
解决方案九:
看不大明白,引用通过hibernate访问数据库时, 是启动的时候, 还是查询的时候?
解决方案十:
NullPointerException,可能是从页面取回的值有等于null的情况