尝试使用JSF,Spring和Hibernate来存储对象时出现NullPointerException

时间:2012-12-14 23:35:05

标签: spring hibernate jsf maven nullpointerexception

我非常擅长使用JSF,Spring,Hibernate和Maven。但到目前为止,我创建了一个简单的项目,在jsf页面上输入名字和姓氏。将它传递给托管bean>然后将它传递给我的服务层的单独模块上的pojo。这一切都有效,但现在我正在尝试将hibernate实现到项目中。我在服务层上向我的pom添加了几个依赖项,在资源文件夹(db.properties,applicationContext.xml,datasource.xml,HibernateSessionFactory.xml)中创建了一些文件,并创建了一个快速的小dao文件。这个体系结构非常糟糕,但我想在设计一个大项目之前先得到一个快速的例子。

我的Web层上的应用程序上下文文件只是将我的托管bean设置为连接到我的服务层。它没有任何hibernate配置,因为我想在我的服务层上进行所有处理和数据库交互。另外,我在我的pojo上使用了hibernate注释。

当我运行我的服务器时,我可以访问我的网页,键入第一个名称并输入最后一个,然后点击提交。当我通过调试时,它将变量从我的托管bean设置到我的服务层,然后我的服务层从customerdao对象调用addCustomer。我在getHibernateTemplate()上得到一个空指针异常.save(customer);线。

WARNING: #{formBean.findBalance}: java.lang.NullPointerException
javax.faces.FacesException: #{formBean.findBalance}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:117)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
    ... 23 more
Caused by: java.lang.NullPointerException
    at com.scott.common.test.Customer.save(Customer.java:52)
    at com.scott.common.CustomerBackingBean.findBalance(CustomerBackingBean.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 24 more

这是我的HibernateSessionFactory.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- Hibernate session factory -->
<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.MySQLDialect</prop>
         <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>

    <property name="annotatedClasses">
    <list>
        <value>com.scott.common.test.Customer</value>
    </list>
    </property> 

</bean>
</beans>

这是我的db.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/scottdb
jdbc.username=djdjdi
jdbc.password=dldljdl

This is my datasource.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean 
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location">
        <value>db.properties</value>
   </property>
</bean>

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
  </bean>

</beans>

这是我的applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

        <bean id="customerDao" 
             class="com.scott.common.test.CustomerDAO" >
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>

</beans>

这是我的客户类

package com.scott.common.test;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


public class CustomerDAO extends HibernateDaoSupport
{   
    public void addCustomer(Customer customer)
    {
        customer.setDescript("Test description");
        getHibernateTemplate().save(customer);
    }
}

2 个答案:

答案 0 :(得分:0)

猜猜 - customer可能是null。尝试进行此更改,检查客户对象是否为空:

public class CustomerDAO extends HibernateDaoSupport
{   
    public void addCustomer(Customer customer)
    {
        if (customer == null ) {
            return; // do nothing if the customer is null
        }
        customer.setDescript("Test description");
        getHibernateTemplate().save(customer);
    }
}

每次调用null对象的方法时,都会得到一个NPE。

另一个选择是抛出一个更有用的异常:

if (customer == null ) {
     throw new IllegalArgumentException("customer may not be null");
}

答案 1 :(得分:0)

只是另一个猜测 - 可能是Spring bean没有正确初始化。 尝试检查:

  1. 是通过Spring创建的customerDAO还是显式新的 CustomerDAO()?
  2. 检入调试器sessionFactory和其他 会话相关的属性。
  3. <强>更新: 在查看代码后,猜测得到了证实:Spring配置错误。 2个eclipse项目中有2个独立的应用程序上下文。需要将其合并为一个(用于Web应用程序)。