08.15.07

Flash CS3 / Flex Error #1009: Cannot access a property or method of a null object reference

Posted in Flash, Flex, errors at 10:06 pm by Curtis J. Morley

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

ActionScript 3 Error #1009 is an AS3 that you can get for many anecdotal reasons. This error basically means that you are trying to reference something that isn't there yet. That something can be a variable, data from the server, or even a movieClip or other display instance. So if you try and make a call to something and are getting this error simply look at what you are trying to reference and ask yourself is it there yet.
Don't bang your head against the wall any longer. Here I explain one of the most un-obvious ways to get this error.

One reason that you will get AS3 Error #1009 is because you are trying to access a movieClip that isn't instantiated yet. For example you try and reference a movieClip named loader_mc that is inside another movieClip. you are referencing it from a class that you created. Each time you run the test the movie you get the Run-Time Error #1009. You look inside your movieClip and see that the loader_mc is right where you want it. It is on frame 10 inside your animated moveiClip. That is when it dawns on you that the call to loader_mc happens when it is instantiated. Simply, put loader_mc on frame 1 and change it's alpha to 0 and then the movieClip will be instantiated when the call from your custom class is made.

Another reason for this error is simply incorrect targeting. If you target parent.bob and the correct reference is parent.parent.bob then you will get this error as well.

Fix 1 :

Make sure the the reference is instantiated before it is called.  This means that the script is run after the object is loaded or the frame in the animation has been reached etc...

Fix 2 :

Check your targeting

Thanks and Happy Flashing

31 Comments »

  1. Nate said,

    August 24, 2007 at 2:13 pm

    I’m new to Flash, and I can’t quite figure out why I would be getting this 1009 error. Do you have any ideas why I might be getting the error with the following code?…

    import flash.events.EventDispatcher;
    import fl.video.*;

    thePlayer.source = “splash.flv”;

    var theCuePoint:Object = new Object();
    theCuePoint.time = 0.001;
    theCuePoint.name = “main_video”;
    thePlayer.addASCuePoint(theCuePoint);

    function splashCuePoint(evt:MetadataEvent):void {
    loadVideo();
    }

    thePlayer.addEventListener(MetadataEvent.CUE_POINT, splashCuePoint);

    function buttonClick(evt:MouseEvent):void {
    loadVideo();
    }

    theButton.addEventListener(MouseEvent.CLICK, buttonClick);

    function loadVideo():void {
    thePlayer.source = “main_video.flv”;
    thePlayer.play();
    theButton.enabled = false;
    theButton.visible = false;
    }

  2. Curtis J. Morley said,

    August 27, 2007 at 5:32 pm

    Nate,

    According to your code you haven’t yet instantiated ‘thePlayer’ or ‘theButton’., unless these are on your stage. If you comment out any of the lines that start with thePlayer. you will see that the error goes away. This may be the reason for the error.

    Tell me how you have your Flash file set up so that I can help you debug a little better.

    Thanks,
    Curtis J. Morley

  3. Nate said,

    August 27, 2007 at 7:32 pm

    I am using an invisible button to keep my main video from downloading, found here…

    http://blog.sessions.edu/web-design/how-to-save-bandwidth-when-displaying-flash-video/

    My goal is to make a video player that acts much like the one that YouTube uses. If you click on the image, the video will load, and if you click on the play button, the video will load.

    I have an FLVPlayback component on the stage on the bottom layer, and an invisible rectangle button on the layer above, over the image portion of the FLVPlayback. The FLVPlayback is named “thePlayer”, and the invisible button is named “theButton”. The actions layer is above the other two, and each layer just has one frame.

    To get the play button to work as I want it to, I have the FLVPlayback component first load the “splash.flv” file, which is just a still image. I then used ActionScript to add a cue point 0.001 seconds into the video. When the cue point hits, it then loads the actual video.

  4. Nate said,

    September 4, 2007 at 7:31 pm

    A fix has been found right here…

    http://www.quip.net/blog/2007/flash/how-to-save-bandwidth-flash-video

  5. scottizzl said,

    September 17, 2007 at 10:54 pm

    thank you! =)

  6. Josh said,

    December 24, 2007 at 8:06 am

    Error 1009 is probably the worst trash error EVER! It doesn’t tell you where the error is. It just says 1009 bla bla bla. I can take like the whole day to fix it. And it’s not because I did not know what I was doing, one moment there is no error, the next, it is there. And I never touched the code of the function that initiated the error. I was working on another part. I tried to block some codes in that function to narrow down where the error was and guess what, the error still appeared no matter what. My head is now bleeding and the wall is demolished and the error is still there.

  7. CK said,

    January 12, 2008 at 1:29 pm

    If you use the mxml argument -verbose-stacktraces=true the error will also output the line number where the null pointer exception was thrown. Seems to be a huge time saver for me while developing.

  8. Bojan said,

    March 13, 2008 at 8:54 pm

    i’m getting this error, because I try to access stage like this “stage.addEventListener(MouseEvent.MOUSE_MOVE, MouseListener);”, but I dont think this is right way to do, but cannot find answer anywhere. Can you help me?..

  9. Curtis J. Morley said,

    March 14, 2008 at 7:21 am

    CK,

    Great tip. Thanks for the comment.

    Josh,

    Sorry about the head trauma man. Often times commenting out code will give this error because it actually makes the value null. Be careful when commenting out code that you are not removing a value that is needed later in the code.

    Bojan,

    The syntax stage.addEventListener(MouseEvent.MOUSE_MOVE, MouseListener); is proper and will work by itself and the function MouseListener. It seems that the error is not on this line but rather somewhere else in the code. If you can give me more code I would be happy to help you out with it.

    Thanks,
    Curtis J. Morley

  10. james said,

    March 16, 2008 at 10:47 pm

    I am getting this error and can’t figure it out. If anyone can help me it would be appreciated.

    TypeError: Error #1009: Cannot access a property or method of a null object reference. at siter_fla::MainTimeline/siter_fla::frame1()

    this error pops up when I enter code. I tested the code by itself in a different flash file and it works fine, but as soon as I add it to my website that has other code in it it stops working and I get this error.

    here is the code I added

    var myloader:Loader = new Loader();

    fe1_btn.addEventListener(MouseEvent.CLICK, image1);
    function image1(event:MouseEvent):void {
    var request:URLRequest = new URLRequest(”1.gif”);
    myloader.load(request);
    addChild(myloader);
    myloader.x=486;
    myloader.y=198;

    }

    fe2_btn.addEventListener(MouseEvent.CLICK, image2);
    function image2(event:MouseEvent):void {
    var request:URLRequest = new URLRequest(”2.gif”);
    myloader.load(request);
    addChild(myloader);
    myloader.x=486;
    myloader.y=198;

    }

  11. Matthew said,

    March 20, 2008 at 2:04 pm

    Hi, I’m new to flash and new to action scripting. I have managed in the past to get a graphic as a button go to a url when pressed. BUt now I have decided to try and use Actionscript 3.0. This was mistake number 1 as it has removed the onPress value from the code, after about half an hour I have come up with the code which seems to work once then comes up with the 1009 error.

    My code is as follows. I have also checked it for errors and it says there is none. I have also made sure that each handler has a different tag as to not get confused.

    Just for you to know each of my images/buttons are labeled as section1 and so on up to 4.

    this.section1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);
    function mouseDownHandler1(event:MouseEvent):void {
    navigateToURL(new URLRequest(”http://www.mysite.com/”));
    }

  12. Matthew said,

    March 20, 2008 at 2:18 pm

    Update: the error I get exactly is:

    Error #1009: Cannot access a property or method of a null object reference.
    at banner_250×240_fla::MainTimeline/banner_250×240_fla::frame27()

  13. kreish said,

    March 26, 2008 at 8:29 am

    help pls

    at frame 1 i put these lines:

    ========================

    btnTest.addEventListener(
    MouseEvent.MOUSE_UP,
    function(evt:MouseEvent):void {
    gotoAndPlay(”Finish”);
    }
    );

    stop();

    ========================

    what if btnTest is on Frame5?
    what will i do to put all AScodes in single frame but then can access symbols or instance names that will load at later frames?

    I do not want to put AS codes where and exaclty the btn will appear.
    :)

    thank u

  14. Ramesh Sakibanda said,

    March 28, 2008 at 4:11 am

    Hi Matthew,

    the reason is, you might not have assigned a variable name to section1

    i.e if you included above script in a frame and that frame *should have* an Object with instance name section1

    thanks
    ramesh

  15. Bojan said,

    April 12, 2008 at 1:28 pm

    Hi again,

    i’m trying in timeline to create new object witch will stop scene playing until I get mouse event(that is just for lerarning, easiest way for me I think).. Here is the code from timeline..

    import vrnjci.uvod.*;

    var zaDugme = new UlaznaKlasa();

    Now, my way that I’m trying to do in constructor is to stop playing the scene, and listener for event like this..

    package com.vrnjci.uvod {
    import flash.display.*;
    import flash.display.Stage;
    import flash.events.*;

    public class UlaznaKlasa extends MovieClip{
    public function UlaznaKlasa(){
    stop();

    addEventListener(MouseEvent.MOUSE_MOVE, MouseListener);
    }

    public function Zavrsi():void{
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, MouseListener);
    }

    private function MouseListener(e:Event):void{
    trace(”RADI!!!!”);
    }

    public function Main():void{

    }
    }
    }

    I haven’t found much useful examples or books online, so any help would be largely appreciated :)..

  16. Mady said,

    May 5, 2008 at 7:01 pm

    Hi guys,

    I’m new to Flash & actionScript 3.0 I keep getting the same error as everyone
    TypeError: Error #1009: Cannot access a property or method of a null object reference.at bhbHome_fla::MainTimeline/frame1()
    I’m building a simple website and trying to control movies with “label frames”

    the first code works fine the button “beer_btn” works fine but when I click on the lightH_btn” it doen’t work, I deleted this last button code and the error went away, which means that that’s where the problem is :(
    If anyone can help me, it will greatly appreciated

    Thanks

    Mady

    here is my code:

    stop();

    function playBeers(event:MouseEvent):void
    {
    this.gotoAndStop(”beers”)
    }

    function playLight(event:MouseEvent):void
    {
    gotoAndPlay(”light”)
    }

    arrowUM.beer_btn.addEventListener(MouseEvent.CLICK, playBeers);

    lightH_btn.addEventListener(MouseEvent.CLICK, playLight);

  17. Cam said,

    May 8, 2008 at 9:28 am

    Hi there Curtis… I am a huge fan of your site and I use it all the time, although this is my first comment. Most of the time I come to your site and I find it very helpful, usually reading your detailed descriptions of errors helps me figure out what is going on with my AS3 code… but Finally I have run into an issue which has completely stumped me! This is why I am posting on your site.

    I have been working on an AS3 version of objects in 3d space, and I have based this on Paul Ortichanian’s version of the code ( http://reflektions.com/miniml/template_permalink.asp?id=459 ) which he developed in the main timeline of an FLA. I have ported the code over to a class, but for some reason I keep getting this error, #1009, which has pretty much become my arch nemesis! Is there any way I could get you to take a quick look at my code to see if you can spot whats wrong?…it would be greatly appreciated!
    feel free to send me an email and I can send over the FLA etc.

    Thanks so much for your help and great work with the site!… you are helping build the AS3 community through your generosity!

  18. Flex Community Blog » Blog Archive » Flex/Flash TypeError: Error #1009: Cannot access a property or method of a null object reference said,

    June 1, 2008 at 10:27 pm

    [...] http://curtismorley.com/2007/08/15/flash-cs3-flex-error-1009-cannot-access-a-property-or-method-of-a... addthis_url = ‘http%3A%2F%2Fblog.flexcommunity.net%2F%3Fp%3D36′; addthis_title = ‘Flex%2FFlash+TypeError%3A+Error+%231009%3A+Cannot+access+a+property+or+method+of+a+null+object+reference’; addthis_pub = ”; [...]

  19. curtismorley.com » Search Engine Optimization Example said,

    June 2, 2008 at 5:51 pm

    [...] Flex Error 1009 - #4 hit on Google [...]

  20. Common ActionScript 3 Errors and Explanations | Jake Rutter - Designer and XHTML/CSS/ActionScript 3 Developer said,

    June 11, 2008 at 10:58 am

    [...] also come up with another great explanation as to what this actually means, you can read about it here. Share and Enjoy: These icons link to social bookmarking sites where readers can share and [...]

  21. Denice said,

    June 13, 2008 at 7:48 am

    Hey.
    I’m getting this error only when I use this script:
    this.introfilmcontainer.skipknap_mc.buttonMode = true;
    introfilmcontainer.skipknap_mc.addEventListener(MouseEvent.CLICK, skipknap);
    function skipknap(e:MouseEvent):void {
    introfilmcontainer.removeChild(introfilm);
    gotoAndPlay(”intro”);
    }
    Its only when I actually USE it. When I don’t use the button everything’s fine…
    What could be my problem?

  22. Curtis J. Morley said,

    June 13, 2008 at 7:55 am

    Denice,

    There are a couple of places that you can get this error. Which line of code is this error on?

    Thanks,

    Curtis J. Morley

  23. Chris Brown said,

    June 18, 2008 at 9:33 am

    Working with external text in flash CS3 to create a website using external txt.

    I am using menu buttons in the main stage to change text the main external text box. Everything works fine. I added a two buttons on the sections stage to change the the text in the external text box. and i get this error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at TAQA_fla::MainTimeline/frame1()

    I have the buttons codes as:

    sections.berry_btn.addEventListener(MouseEvent.CLICK,b8Listener);
    sections.skinner_btn.addEventListener(MouseEvent.CLICK,b9Listener);

    then:

    function b8Listener(event:MouseEvent):void {
    loadFile(”berry.txt”);
    sections.gotoAndStop(”artist”);
    }

    function b9Listener(event:MouseEvent):void{
    loadFile(”skinner.txt”);
    sections.gotoAndStop(”skinner”);
    }

    Do you need to see the whole code. What do i need to get my buttons to work with out having them on the main stage.

  24. Peter Kahuria said,

    June 21, 2008 at 8:17 pm

    Man, thanks alot! I have been scratching my head over this error and I was even considering going back to using AS 2.0.

    I had movie clips inside another movie clip and your explanation really hit home.

    Thank you again!

  25. Curtis J. Morley said,

    June 21, 2008 at 8:58 pm

    Peter,

    I am glad that this helps. It is worth the extra effort to get the benefits of AS3. Hopefully I will be of more help for you in te future.

    Thanks,
    Curtis J. Morley

  26. Gianluca Bruno said,

    July 8, 2008 at 4:11 pm

    I am getting the 1009 error doing something a little differently from others. I have posted my question on the adobe forums, but haven’t able to get a response. I have a button click action:

    The logon function

    private function logon():void
    {
    userSession.userId = userId.text;
    userSession.password = password.text;
    var token:AsyncToken = UserSessionAssembler.createItem(userSession);
    token.operation = “CREATE”;
    }

    The userSession object is being created in the init method, and that is being called on creationComplete

    private function init():void
    {
    userSession = new UserSession();
    }

    The UserSession class is an actionscript class which I created and is mapped to a java class.

    package assets.actionscript
    {
    [Bindable]
    [RemoteClass(alias="UserSession")]
    public class UserSession
    {
    public var userId:String;
    public var password:String;
    public var status:String;

    public function UserSession()
    {
    }

    public function getStatus():String {
    return this.status;
    }

    public function setStatus(status:String):void {
    this.status = status;
    }

    public function getUserId():String
    {
    return this.userId;
    }

    public function setUserId(userId:String):void
    {
    this.userId = userId;
    }

    public function getPassword(password:String):String
    {
    return this.password;
    }

    public function setPassword(password:String):void
    {
    this.password = password;
    }
    }
    }

    If I call the create method directly in the click function I can pass the object to the java class fine and it works, but for some reason when the object userSession is passed to the create method it throws the 1009 error. I have been stuck on this for a few days and has been very frusturating trying to resolve this.

  27. Gianluca Bruno said,

    July 8, 2008 at 4:12 pm

    Sorry for some reason some of my code from the previous post did not submit

    click=”logon()”

  28. robert said,

    July 10, 2008 at 4:42 am

    hi,
    I’m getting the 1009 error whie accessing the button instance. everything looks fine, and i can’t figure it out. here’s the code:

    apps.addEventListener(MouseEvent.CLICK, clickapps);
    function clickapps(event:Event):void{
    trace(”apps”);
    gotoAndStop(”apps”);
    }
    entrees.addEventListener(MouseEvent.CLICK, clickentrees);
    function clickentrees(event:Event):void{
    trace(”entrees”);
    gotoAndStop(”entrees”);
    }

    this is the full error message:

    apps
    TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::MovieClip@1ec786a1 to flash.display.SimpleButton.

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at barcelona_fla::MainTimeline/barcelona_fla::frame10()

    thanks for your help.
    robert

  29. Curtis J. Morley said,

    July 10, 2008 at 6:43 am

    Robert,

    I tried your code with 2 movieClips on the stage in all frames. With your code the example works fine for me. Because of this I would assume (without being able to see your whole file) that a movieClip is not on the stage or something similar.

    Actually, the second error may disappear if you resolve the first. It sounds like a movieClip is trying to be turned into a button.

    Thanks,

    Curtis J. Morley

  30. eduardo said,

    July 22, 2008 at 1:00 pm

    can you tell me how do i aquire success on this function:

    tryin to parse the tweener target dynamically

    import caurina.transitions.*;
    var countNumber;
    var tgt:String;
    function go():void {
    for (count = 1; count < 10; count++) {
    tgt = ‘e’ + conta;
    Tweener.addTween(tgt, { ……. });
    }
    }

  31. Scott said,

    August 12, 2008 at 7:35 pm

    Can anyone help me? I am very green at this and am just trying to do a simple, simple, simple flash site but I cannot get around this 1009 bug. It’s awful and stopping me from getting anywhere.

    Basically how do you get the program to recognize the button as not null. I guess I mean how do you get it to read the button then apply the Action script in the proper order or vice versa whcih ever is correct. I can’t believe Adobe designed this flaw into the program. Horrible! And when i say simple I mean I don’t understand hardly any of the code above. I’m really depressed over this- ruining my efforts t get a site up- i’d made such good progress til now. Any help would be sooooo appreciated.
    The code I am using is below:

    stop();

    FsAs.addEventListener(MouseEvent.CLICK, FestsAwards);
    function FestsAwards(event:MouseEvent):void {
    gotoAndPlay(4)
    }

    CsBs.addEventListener(MouseEvent.CLICK, ContsBios);
    function ContsBios(event:MouseEvent):void {
    gotoAndPlay(35)
    }

    Imgs.addEventListener(MouseEvent.CLICK, Images);
    function Images(event:MouseEvent):void {
    gotoAndPlay(61)
    }

    CstCrw.addEventListener(MouseEvent.CLICK, CastCrew);
    function CastCrew(event:MouseEvent):void {
    gotoAndPlay(111)
    }

    BlgNws.addEventListener(MouseEvent.CLICK, BlogNews);
    function BlogNews(event:MouseEvent):void {
    navigateToURL(new URLRequest(”http://www.shores.blogspot.com/”));
    }

Leave a Comment