Announcement Announcement Module
No announcement yet.
Trouble with events Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trouble with events

    I'm in trouble publishing an event. After write both the event class and the listener class, and register in the configuration file both the listener class and the regular class (to be aware of the container and allow the proper injection) I try to publish the event but a NullPointerException is thrown:
    import org.springframework.context.ApplicationEventPublisher;
    import org.springframework.context.ApplicationEventPublisherAware;
    public class RegularClass implements ApplicationEventPublisherAware {
    	private ApplicationEventPublisher aep;
    	public void setApplicationEventPublisher(ApplicationEventPublisher aep) {
    		this.aep = aep;
    		// Checks if the ApplicationEventPublisher has been injected succesfully
    		System.out.println("#1 : " + (this.aep != null));
    	public void methodThatSendsAnEvent() {
    		MyEvent event = new MyEvent(this, "send by " + this.getClass().getName());
    		System.out.println("#2 : " + (this.aep != null));
                    // Here is where the exception is thrown
    import org.springframework.context.ApplicationContext;
    public class Main {
    	public static void main(String[] args) throws Exception {
    		ApplicationContext context = 
    			new ClassPathXmlApplicationContext("beans.xml");
    		RegularClass rc = new RegularClass();
    The console output is:
    #1 : true
    #2 : false
    Exception in thread "main" java.lang.NullPointerException
    at RegularClass.methodThatSendsAnEvent(RegularClass.j ava:20)
    at Main.main(

    Appears as the ApplicationEventPublisher being injected succesfully by the container (the bean definition for RegularClass is also declared with the dependency-check="objects" attribute to ensure the success of the injection), but when the publishEvent() method is called gives back a null value for this ApplicationEventPublisher instance.
    Last edited by davidmarco; Nov 3rd, 2009, 04:49 PM.

  • #2
    Technically, you don't register a "class" in the Spring context, you register a bean, and you specify the class of the bean. That distinction is important here, because when you manually created the "RegularClass" instance in your "main" method, you completely bypassed the work that the Spring container would do to initialize your dependencies.

    What you have to do is actually use that "context" variable that you set in the "main" method, and get the RegularClass bean that you declared in the Spring context (as you don't show your Spring context, I don't know what that name would be).

    You would do this with something like this:

    RegularClass rc = (RegularClass) context.getBean("whatevernameyougavetheRegularClas sbean");


    • #3
      Thanks dkarr, I still have too many old java in mind and I forgotten retrieve the bean from the application context.