Search code examples
actionscript-3flash-cs5typeerrordocument-class

Flash TypeError: Error #1009: Cannot access a property or method of a null object reference. - when accessing TLFTextField from Document Class


I'm lost on this one. I receive a TypeError: Error #1009: Cannot access a property or method of a null object reference. output message the first time my Document Class tries to access a simple textfield on the stage (added from the IDE, not actionscript)

package  {

import flash.display.*;
import fl.text.*;
import flash.text.*;
import flash.events.*;
import flash.net.*;

public class Main extends MovieClip {

    private var _netConnection:NetConnection;
    private var _responder:Responder;
    /* some other public + private vars */

    public function Main() {
        init();
    }

    public function init(e:*=null):void {
        _netConnection = new NetConnection();
        _responder = new Responder(uponResult);

        txt.text = "init()";
    }
    /* more functions */
  }
}

I tried adding txt.addEventListener(Event.ENTER_FRAME, init); incase the txt TLFTextField wasn't ... there... at the beginning, but it still outputs the error.

I feel like a bit of an idiot atm, what's the prognosis doc? JB


Solution

  • TLFTextFields are weird creatures, I've had a lot of issues with them recently.

    I'd try to use the Event.ADDED_TO_STAGE event because the TLFTextFields have to be on the stage when you try to access them:

    public function Main() {
      addEventListener(Event.ADDED_TO_STAGE, init);
    };
    public function init(e:Event):void {
      removeEventListener(Event.ADDED_TO_STAGE, init);
      txt.text = "init()";
    };
    

    It should work if your TLFTextField is on the first frame on the Main Timeline.

    Let me know if this one does the magic,

    Rob