Search code examples
apache-flexflashactionscript-3mxmlcairngorm

Why is my object reference null when trying to reference an mx:Canvas from ActionScript?


this is a follow up question from this one, I don't want to keep going in the comments and preventing people from getting hard-earned reputation... :)

In my Cairngorm command class, to get it to compile I needed to tell it what myCanvas was, so I used this line:

var myCanvas : MyCanvas = new MyCanvas;

I'm guessing that's wrong, though, because although it compiles, if I try to do something like this:

if (myCanvas.subObject.value == 0) { ... }

it'll throw this error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at com.foo.bar.command::MyCommand/execute()

as if the subObject doesn't exist. It looks like I might be getting a new instance of MyCanvas, not the instance I want from the main.mxml with an id of myCanvas. Am I right? How do I fix this?

Edit (10:59pm GMT+1): Okay, so it looks like I've been way too vague here. Here's my main.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:control="com.foo.bar.control.*" 
    xmlns:business="com.foo.bar.business.*"
    xmlns:view="com.foo.bar.view.*"
    applicationComplete="com.foo.bar.util.StartupUtil.init()"
    horizontalScrollPolicy="off"
    verticalScrollPolicy="off"
    borderThickness="0"
    paddingBottom="0"
    paddingLeft="0"
    paddingTop="0"
    paddingRight="0"
>
    <mx:Script>
        <![CDATA[
            import com.foo.bar.model.PlayerModelLocator;

            [Bindable]
            private var model : PlayerModelLocator = PlayerModelLocator.getInstance();
        ]]>
    </mx:Script>

    <!-- ========================================================================== -->

        <!-- the ServiceLocator where we specify the remote services -->
        <business:Services id="services" />

        <!-- the FrontController, containing Commands specific to this application -->
        <control:PlayerController id="controller" />

    <!-- ========================================================================== -->

  <mx:Style source="assets/main.css" />
  <view:MyCanvas id="myCanvas" /> 

</mx:Application>

And here's my com/foo/bar/command/MyCommand.as:

package com.foo.bar.command {
    /* add to controller
    addCommand( MyEvent.EVENT_CHANGE_VOLUME, ChangeVolumeCommand );
    */
    import flash.net.SharedObject;
    import com.adobe.cairngorm.control.CairngormEvent;
    import com.adobe.cairngorm.commands.ICommand;

    import com.foo.bar.model.PlayerModelLocator;
    import com.foo.bar.event.MyEvent;
    import com.foo.bar.view.*;

    public class ChangeVolumeCommand implements ICommand {
        public function execute(event:CairngormEvent):void {
      var model : PlayerModelLocator = PlayerModelLocator.getInstance();
      var myEvent : MyEvent = MyEvent(event);
      var myCanvas : MyCanvas = new MyCanvas();
      var so:SharedObject = SharedObject.getLocal("fixie.video");
      if (myCanvas.subObject.value == 0) {
        trace("subobject value is 0");
      }
        }
    }
}

Basically, I want to get a handle on the object with ID myCanvas in main.mxml using the myCanvas object in MyCommand.as


Solution

  • What is the scope of your myCanvas variable? Is it inside a method somewhere? You will need to make it public or give it getter/setter to be able to access it.

    You may also be trying to reference it before it has been added to its parent, using the addChild() method.

    There really isn't enough code in your examples to determine the problem, but these things should give you somewhere to start looking.