hallyhfk
2008-08-14 10:53 阅读 314
已采纳

hibernate Unhandled Exception thrown: class java.l

在通过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的这个配置。
疯了。
十分感谢小疯子的帮助。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

10条回答 默认 最新

  • 已采纳
    iteye_14762 iteye_14762 2008-08-15 10:47

    你用的是这个Action吗:
    [code="java"]
    class="com.afa.struts.action.Test1Action">

    [/code]
    怎么没有看到comservice注入?
    如果你是想继承baseAction, 也没有看到你继承的配置啊?

    点赞 评论 复制链接分享
  • beijing_2008_2008 beijingpz 2008-08-14 11:03

    NullPointerException,可能是从页面取回的值有等于null的情况

    点赞 评论 复制链接分享
  • iteye_14762 iteye_14762 2008-08-14 11:05

    看不大明白,[quote]通过hibernate访问数据库时[/quote], 是启动的时候, 还是查询的时候?

    点赞 评论 复制链接分享
  • alongytl alongytl 2008-08-14 11:11

    这个提示太少了,建议自己debug一下.

    点赞 评论 复制链接分享
  • wxjiaaa wxjiaaa 2008-08-14 12:17

    空指针异常

    具体是哪里那你得把完整的报错信息发上来, 起码得以你发的这句的为中心 上下5行内的发过来吧

    点赞 评论 复制链接分享
  • iteye_14762 iteye_14762 2008-08-14 12:39

    建议将的查询的那段代码贴出来, 看是不是hql, 或者是传递参数有问题.完整的异常信息最好也贴上来!

    点赞 评论 复制链接分享
  • Qonfire Qonfire 2008-08-14 13:42

    "WARN action.RequestProcessor - Unhandled Exception thrown: class java.lang.NullPointerException "

    昨天刚调试一个这样的错误。问题出在当一个对象的实例 object为NULL时,调用该object的getXXX()或者setXXX()时,就报这个错误。

    点赞 评论 复制链接分享
  • iteye_14762 iteye_14762 2008-08-14 20:04

    楼主能不能把Test1Action的execut的方法贴出来, 主要看下19行是怎么写的[code="java"]at com.afa.struts.action.Test1Action.execute(Test1Action.java:19)[/code]

    点赞 评论 复制链接分享
  • iteye_14762 iteye_14762 2008-08-15 09:54

    [code="java"]
    ComDaoIMPL comservice=new ComDaoIMPL();
    [/code]
    不应该是new的, 这样的话comservice中的comdaoimpl没有初始化!应该直接用spring注入进来:
    [quote]
    ......
    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");
    }

    [/quote]
    你的action交给spring管理, 然后注入comService!

    点赞 评论 复制链接分享
  • iteye_14762 iteye_14762 2008-08-15 09:54

    [code="java"]
    ......
    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");
    }
    [/code]

    点赞 评论 复制链接分享

相关推荐