1

I have TextField instance called inputWord which contains no text on the first frame. On the same frame, on the actions layer, any time when I refer to inputWord in any way, there is an error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at DC/frame1()[DC::frame1:19] //DC is the name of document class that I created.
at flash.display::MovieClip/gotoAndStop()
at DC()[C:\Users\nikkka\Desktop\flash\DC.as:25]

19 is the number of line where my code that involves inputWord is located. It works, I mean I write inputWord.text = "smth"

it text becomes "smth" but there is the same error. Why?

nicks
  • 2,161
  • 8
  • 49
  • 101
  • Have you made sure the component has been rendered? – Kevin Aug 05 '11 at 08:45
  • Too bad you're using Flash and not Flex. In Flex you could listen for the `CREATION_COMPLETE` event but, since you're using Flashm you could use the `Timer` class or the `setTimeout(..)` function just to check if the component will be render later on. – Kevin Aug 05 '11 at 08:50
  • 1
    SOLVED. That's what strange. I had `stop()` in the beginning of the frame. I moved it to the end and everything worked fine. Wounder, why's that?... – nicks Aug 05 '11 at 08:59

2 Answers2

2

The problem is with gotoAndStop()

in as2, when you do a gotoAndStop you can access the resources in the frame right away, as Kevin pointed out, the frame has to be rendered first

to do this, you need to use an onrender listener to fire when you rendered the frame to deal with the frame related logic. Then you need to invalidate the stage, to force the rendering to fire.

like so:

stage.addEventListener(Event.RENDER, onRenderStage);
protected function onRenderStage(ev:Event):void {
    inputWord.text = "smth"
    trace(inputWord.text);
}
gotoAndStop(5);
stage.invalidate();
Daniel
  • 34,125
  • 17
  • 102
  • 150
1

Probably on the first frame, inputWord is not loaded yet so you get an error. On the next frames, it is loaded so the text is being set successfully. The solution is test for the existence of the text field before setting it:

if (this.inputWord) this.inputWord.text = "smth";
laurent
  • 88,262
  • 77
  • 290
  • 428