Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用JSF、Spring和Hibernate存储对象时出现NullPointerException_Spring_Hibernate_Jsf_Maven_Nullpointerexception - Fatal编程技术网

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

尝试使用JSF、Spring和Hibernate存储对象时出现NullPointerException,spring,hibernate,jsf,maven,nullpointerexception,Spring,Hibernate,Jsf,Maven,Nullpointerexception,我对使用JSF、Spring、Hibernate和Maven非常陌生。但到目前为止,我创建了一个简单的项目,在jsf页面上输入了名字和姓氏。将其传递给托管bean>,然后将其传递给我的服务层的单独模块上的pojo。这一切都是可行的,但现在我正试图在项目中实现hibernate。我在服务层的pom中添加了几个依赖项,在资源文件夹中创建了一些文件(db.properties、applicationContext.xml、datasource.xml、HibernateSessionFactory.x

我对使用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);线路

这是我的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>

只是猜测-
customer
可能是
null
。尝试进行此更改,检查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);
    }
}
任何时候当你在一个空对象上调用一个方法时,你都会得到一个NPE

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

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

只是另一种猜测——可能是Springbeans没有正确初始化。 尝试检查:

  • customerDAO是通过Spring还是其他明确的新工具创建的 CustomerDAO()
  • 签入调试器sessionFactory和其他 会话相关属性
  • 更新
    在查看代码后,猜测被证实:Spring配置错误。在2个eclipse项目中有2个独立的应用程序上下文。需要将其合并为一个(适用于web应用程序)。

    您转储所有这些,但不知道引发异常的位置。尝试提交空指针异常。下面是堆栈跟踪的前两行:警告:#{formBean.findBalance}:java.lang.NullPointerException javax.faces.FacesException:#{formBean.findBalance}:java.lang.NullPointerException如果您看到stacktrace formBean中的上述指示按照我的猜测为空,则可能是依赖注入问题。你能不能删除
    pom.xml
    ,因为这篇文章没有用,请发布完整的
    stacktrace
    和JSF spring集成相关的东西。你需要显示完整的stacktrace。我用完整的stacktrace更新了这个,谢谢你的快速回复。客户实际上得到了所有正确的数据。我想,也许我的会话工厂没有设置好,但这只是一个完全的猜测。这是堆栈跟踪的前两行。警告:#{formBean.findBalance}:java.lang.NullPointerException javax.faces.FacesException:#{formBean.findBalance}:java.lang.NullPointerException CustomerDao正在我的客户pojo中显式设置(正如我所说的,体系结构只是基本的pojo和dao层,并不是为了成为一个好的设计而编写的,只是想学习如何让spring、jsf和hibernate协同工作).调试后,我可以看到sessionFactory未设置。我可以/应该如何设置此设置,还有哪些其他方法可以检查我的SpringBean是否已正确初始化?再次感谢您的所有帮助您应该注入customerDao(最佳方法),或者从应用程序上下文中读取customerDao/sessionFactory。好的,我可以注入dao(我假设这只是通过在DAOBean声明下设置属性和ref实现的。与我执行sessionFactory的方式相同)。但如何才能使bean初始化?我知道我可以使用Applicationcontext context=new ClassPathXmlApplicationContext(“path/to/file/”)在我的主方法中的一个简单java项目中。但当我在dao中尝试这样做时,基本上有一个无限的错误循环。您确定上下文没有正确启动吗?对于web应用程序,它是在springapp-servlet.xml(Tomcat)中创建的,很容易找到示例。不要使用ClassPathXmlApplicationContext。我有两个单独的模块(项目)。我的webapp是jsf,它有一个托管bean、applicationContext和其他必需的文件。此模块工作并设置属性,并将变量从托管bean发送到单独模块中的pojo。此单独模块有两个类(customer和customerDAO),db.properties,HibernateSessionFactory.xml,ApplicationContext.xml和DataSource.xml。当托管bean中的变量传递到客户类(pojo)时,它设置值,然后调用CustomerDAO.addcustomer(客户)。在dao中,它执行getHibernateTemplate.save(客户)。
     <?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);
        }
    }
    
    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);
        }
    }
    
    if (customer == null ) {
         throw new IllegalArgumentException("customer may not be null");
    }