# DispatchEvent Confusion?

**URL:** <https://forum.kirupa.com/t/dispatchevent-confusion/302894>\
**Category:** Uncategorized\
**Created:** [January 9, 2010, 2:28pm UTC](https://forum.kirupa.com/t/dispatchevent-confusion/302894 "2010-01-09T14:28:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ttmt](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@ttmt](https://forum.kirupa.com/u/ttmt)\
**Post date:** [January 9, 2010, 2:28pm UTC](https://forum.kirupa.com/t/dispatchevent-confusion/302894/1 "2010-01-09T14:28:59Z")

</div>

DispatchEvent Confusion ?

Hi all

I’m trying to understand dispatchEvent, so I’m trying to write a little example. I have 3 classes.

1. Document Class - to start it off.
2. Dispatcher Class - to dispatcher the Event
3. Listener Class - to listen for the dispatched event, and produce a trace to confirm.

The code just gives me a TypeError -

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Which I think means I’m trying to reference something that isn’t there yet - How do I ensure the classes are there to be used ?

Any help on this would be really appreciated - I just want a simple working example to understand how DispatchEvent works.

```auto

Document.as

package{
	
	import flash.display.MovieClip
	
	import Dispatcher
	import Listener
	
	public class Document extends MovieClip{
		
		private var _lis:Listener
		private var _dis:Dispatcher
		
		public function Document(){
			
			_lis = new Listener()
			_dis = new Dispatcher()
			
			_dis.sendOut()
			
		}
		
	}
}

```

```auto

Dispatcher.as

package{
	
	import flash.events.*
	
	public class Dispatcher extends EventDispatcher{
		
		public static const MESSAGE:String = "update"
		
		public function Dispatcher(){
			
		}
		
		public function sendOut(){

			dispatchEvent(new Event(Dispatcher.MESSAGE))
			
		}
		
	}
}

```

```auto

Listener.as

package{
	
	import flash.events.*
	
	import Dispatcher
	
	public class Listener{
		
		private var _dis:Dispatcher
		
		public function Listener(){
					
			_dis.addEventListener(Dispatcher.MESSAGE, myFun)
			
		}
		
		public function myFun():void{
			trace("Message received")
			
		}
		
		
	}
}

```
