Skip to main content
Inspiring
April 22, 2013
Question

How to get text from textfield on the stage in Main class to external class

  • April 22, 2013
  • 1 reply
  • 1143 views

Hello, I've got little (I hope) problem.

I don't know how to get text from text field that is on the stage to external class code.

I think it might look like this:

Main class:

public var username:Textfield = username_txt;

External class:

import Main;

private var _mainClass:Main;

// place where I need to get the text

vars.username = _mainClass.username.text;

but code above is not working because I'm getting this error:

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

Thank you so much in advance for all help

This topic has been closed for replies.

1 reply

Inspiring
April 22, 2013

you don`t need these lines:

import Main;
private var _mainClass:Main;

if your external class has acces to the stage/displayList

you can always call your var with either

MovieClip(root).username (absolute syntax)

or in your case with

MovieClip(this.parent).username

be aware that you have to have either already a instance of External class on the stage for this to work,

or if you use addChild, wait until the constructor has access to stage:

public function ExternalClass():void{

     if(stage) init();

    else{

addEventListener(Event.ADDED_TO_STAGE, init);

    }

private function init(e:Event = null):void{

//put your code here

}

}

Inspiring
April 22, 2013

hi, I've tryed that, but I'm getting these errors:

/Users/Peter/Desktop/project/project beta 0.6/as3/com/filebrowser/FileListManager.as, Line 401180: Call to a possibly undefined method MovieClip.

/Users/Peter/Desktop/project/project beta 0.6/as3/com/filebrowser/FileListManager.as, Line 401119: Access of possibly undefined property parent through a reference with static type com.filebrowser:FileListManager.

line 40 is this:

vars.username = MovieClip(this.parent).username;

ps. thanks for your helping to me

Inspiring
April 23, 2013

Put this import statement at the beginning of your FileListManager-class

import flash.display.MovieClip

another problem is that your FileListManager-class ist static, so you can`t use the this keyword

Be sure thatz you want to use a static class, because it has some restrictions