DispatchEvent Confusion?

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.


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()
			
		}
		
	}
}


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))
			
		}
		
	}
}


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")
			
		}
		
		
	}
}