Announcement Announcement Module
Collapse
No announcement yet.
NullPointer Exception accesing a variable who should be injected by Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NullPointer Exception accesing a variable who should be injected by Spring

    Hi all,

    I'm trying to construct my Services layer to access model.
    When I call a service's method to get something contained on DB, I get the following exception:
    Code:
    Stacktraces
    java.lang.NullPointerException
    
        services.Services.sess(Services.java:23)
        services.Services.getUsers(Services.java:32)
        actions.access.Login.execute(Login.java:17)
    ...
    Services.java code:
    Code:
    package services;
    
    import org.springframework.transaction.annotation.Transactional;
    import org.hibernate.SessionFactory;
    import org.hibernate.Session;
    import data.*;
    
    import java.util.List;
    
    // This class is the business services tier in the application.
    // @Transactional is needed so that a Hibernate transaction is set up,
    //  otherwise updates won't have an affect
    @Transactional
    public class Services {
    	// So Spring can inject the session factory
    	SessionFactory sessionFactory;
    	public void setSessionFactory(SessionFactory value) {
    		sessionFactory = value;
    	}
    
    	// Shortcut for sessionFactory.getCurrentSession()
    	public Session sess() {
    		return sessionFactory.getCurrentSession();
    	}
    
    	public Webuser getEventByNickname(String nickname) {
    		return (Webuser) sess().load(Webuser.class, nickname);
    	}
    	
    	@SuppressWarnings("unchecked")
    	public List<Webuser> getUsers() {
    		return sess().createQuery("from Webuser").list();
    	}
    }
    Exception is raised on sessionFactory.getCurrentSession() call, in sess() method. So sessionFactory is null, but it supossed Spring had to injects the value earlier. It makes me think I'm doing something wrong.

    That's my applicationConfig.xml:
    Code:
    <?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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
    	<!-- The singleton hibernate session factory -->
    	<bean id="sessionFactory" scope="singleton"
    		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="configLocation" value="classpath:hibernate.cfg.xml" />
    	</bean>
    
        <!-- Spring's hibernate transaction manager, in charge of making hibernate sessions/txns -->
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    
        <!-- So classes/functions with @Transactional get a hibernate txn -->
    	<tx:annotation-driven />
    	
    	<!-- Inject my business services class to the actions -->
    	<bean id="services" class="services.Services" scope="singleton">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    	
    </beans>
    And my hibernate.cfg.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    
    	<session-factory>
    	
    		<!-- Database connection settings -->
    		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    		<property name="connection.url">jdbc:mysql://localhost/........</property>
    		<property name="connection.username">.....</property>
    		<property name="connection.password">.......</property>
    		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    	
    		<!--
    		Enable c3p0 connection pooling, because hibernate pooling is not
    		prod-ready. Apparently connection.provider_class is needed in
    		hibernate 3+
    		-->
    		<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    		<property name="c3p0.max_size">100</property>
    		<property name="c3p0.min_size">1</property>
    		<property name="c3p0.idle_test_period">30</property>
    	
    		<!-- Echo all executed SQL to stdout for debugging -->
    		<property name="show_sql">true</property>
    		
    		<!-- All the entity classes for hibernate to check for annotations here -->
    		<mapping class="data........." />
    		<mapping class="data........." />
    
    	</session-factory>
    
    </hibernate-configuration>
    If you need more information to help me to solve the problem, please don't hesitate to ask for it.

    Thanks in advance

  • #2
    My guess you are constructing you rown instance inside your logni action...

    Comment


    • #3
      You are absolutely right

      Thanks a million!

      Comment

      Working...
      X