Results 1 to 3 of 3

Thread: NullPointerException when using JpaTemplate

  1. #1
    Join Date
    Mar 2008
    Posts
    3

    Default NullPointerException when using JpaTemplate

    I am using MySQL, Hibernate 3, Eclipse 3.2.

    Here is the Persistence XML:
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    <persistence-unit name="registrationPU">
    <class>com.bookstore.model.Registration</class>
    <properties>
    <!-- If the table is to be created from the class
    <property name="hibernate.hbm2ddl.auto" value="create"/> -->
    </properties>
    </persistence-unit>
    </persistence>


    Here is the contectfile:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean
    class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor" />

    <bean id="registrationServices"
    class="com.bookstore.dao.hibernate.RegistrationDAO Hibernate">
    <property name="jpaTemplate" ref="jpaTemplate"/>
    </bean>

    <bean id="jpaTemplate"
    class="org.springframework.orm.jpa.JpaTemplate">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource">
    <ref local="dataSource" />
    </property>

    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </prop>
    </props>
    </property>
    </bean>

    <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="registrationPU" />
    <property name="jpaVendorAdapter">
    <bean
    class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter">
    <property name="database" value="MYSQL" />
    <property name="showSql" value="true" />
    </bean>
    </property>
    </bean>

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/struts2webappdb" />
    <property name="username" value="elan" />
    <property name="password" value="elan" />
    </bean>

    <bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionM anager">
    <property name="entityManagerFactory"
    ref="entityManagerFactory" />
    </bean>
    </beans>

    Here is the Dao query:
    public Registration findByEmailAddressAndPassword(String emailAddress, String password) {

    String query = "from Registration as registration where registration.emailAddress = ?1 and registration.password = ?2";
    Registration registration = null;
    Object[] parameters = { emailAddress, password };
    try {
    List list = this.getJpaTemplate().find(query, parameters);
    }
    catch (Exception e)
    {System.out.println (e);}
    if (list.size() > 0) {
    registration = (Registration) list.get(0);
    }
    return registration;
    }

  2. #2

    Default

    Where is the NPE ?

    Is it by chance in the JpaTemplate.find(final String queryString, final Object... values) method? Try using the method with the Map instead. Also, not sure about the validity using the class name as the variable name just lower cased.

    A stack trace would be useful.

  3. #3
    Join Date
    Mar 2008
    Posts
    3

    Default

    Thanks for your suggestion. I already solved the problem.
    The problem was that I had no applicationContext object because it was atest prgram that was not triggered by an Action. I have added a creation of a contextApplication object and all is working fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •