4 Replies Latest reply on Feb 7, 2008 2:57 PM by ntsiii

    Cannot access a property or method of a null object reference

    dubbeat
      Hi,

      Im new to flex and am having a problem with implenting my code.

      I wrote a few simple classes to make up a simple model view controller.

      In my main MXML file I try to instantiate everything like so.

      --------------------------------------------------------------------
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationPolicy="all" creationComplete="start();">
      <mx:Script>
      <![CDATA[
      import flash.display.*;
      import flash.events.*;
      public function start():void
      {
      var model:IModel=new Model();
      var controller:IKeyboardInputHandler=new Controller(model);
      var view:View=new View(model,controller,this.stage);
      //trace("start");
      }
      ]]>
      </mx:Script>
      --------------------------------------------------------------------

      The code works fine as a document class in flash (without the mx:script stuff of course).
      when I try to run this I get the following error


      -------------------------------------------------------
      TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at View$iinit()
      at MVC_Player/start()
      at MVC_Player/___Application1_creationComplete()
      at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
      at flash.events::EventDispatcher/dispatchEvent()
      at mx.core::UIComponent/dispatchEvent()
      at mx.core::UIComponent/set initialized()
      at mx.managers::LayoutManager/::doPhasedInstantiation()
      at Function/ http://adobe.com/AS3/2006/builtin::apply()
      at mx.core::UIComponent/::callLaterDispatcher2()
      ---------------------------------------------------------

      Can anybody help me understand whats going on and how to fix it?

      Thanks,
      dub

      (p.s. sorry but I couldnt see how to attach code in the forum)
        • 1. Re: Cannot access a property or method of a null object reference
          Gregory Lafrance Level 6
          Usually this occurs because an instance of some object has not been created yet. Post the whole code for a simplified case and we can help. Not being able to post code without converting <, >, &, ", etc. to XML entities is a forum bug that is known and is to be addressed soon.
          • 2. Re: Cannot access a property or method of a null object reference
            dubbeat Level 1
            Hi

            Thanks for the reply
            Here is the code that I have.

            Like I said, the code works in flash. I'm pretty in experienced with flex so I not sure where to begin investigating. Its the main MXML thats giving me trouble.



            // Model Interface
            package
            {

            import flash.events.*;
            import flash.events.IEventDispatcher;

            public interface IModel extends IEventDispatcher
            {
            function setKey(key:uint):void
            function getKey():uint

            }
            }



            // ActionScript file
            //Contorller Interface
            //simple class of "CONTROLLER". Each Controller is a concrete strategy
            package{

            import flash.events.*;


            public interface IKeyboardInputHandler
            {
            function keyPressHandler(event:KeyboardEvent):void

            }


            }



            // ActionScript file
            //Model Class
            package{
            import flash.events.EventDispatcher;
            import flash.events.*;


            public class Model extends EventDispatcher implements IModel
            {
            private var lastKeyPressed:uint=0;

            public function setKey(key:uint):void
            {
            this.lastKeyPressed=key;
            dispatchEvent(new Event(Event.CHANGE));

            }
            public function getKey():uint
            {
            return lastKeyPressed;
            }
            }
            }


            // ActionScript file
            //view class
            //View is a COncrete Observer and Strategy "context"

            package
            {


            import flash.events.*;
            import flash.display.*;

            public class View
            {

            private var model:IModel;
            private var controller:IKeyboardInputHandler;

            public function View(aModel:IModel, oController:IKeyboardInputHandler, target:Stage)
            {
            this.model=aModel;
            this.controller=oController;

            //recieve notifications from the model
            model.addEventListener(Event.CHANGE,this.update);

            //revcieve key presses from stage

            target.addEventListener(KeyboardEvent.KEY_DOWN,this.onKeyPress);

            }

            private function update(event:Event):void
            {
            //get data from model and update view
            trace("the key pressed was"+model.getKey());
            }

            private function onKeyPress(event:KeyboardEvent):void
            {
            //delegate to the controller (strategy) to handle it
            controller.keyPressHandler(event);

            }


            }

            }


            // ActionScript file
            //controller class
            package
            {

            import flash.events.*;

            public class Controller implements IKeyboardInputHandler
            {
            private var model:IModel;

            public function Controller(aModel:IModel)
            {
            this.model=aModel;

            }

            public function keyPressHandler(event:KeyboardEvent):void
            {
            model.setKey(event.charCode);//change the model
            trace("press");

            }


            }

            }





            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="start()">


            <mx:Script>
            //MAIN
            <![CDATA[
            public function start():void
            {
            var model:IModel=new Model();
            var controller:IKeyboardInputHandler=new Controller(model);
            var view:View=new View(model,controller,this.stage);

            }
            ]]>
            </mx:Script>
            </mx:Application>
            • 3. Re: Cannot access a property or method of a null object reference
              dubbeat Level 1
              In the MXML file on the third line "creationComplete" it should say =start().

              It just came out all funny
              • 4. Re: Cannot access a property or method of a null object reference
                ntsiii Level 3
                First, debug this to find out which object is null.

                Step through it in the debugger, or put trace statemsnts between each line.

                Tracy