Reputation: 21
I have a class, my document class, called SilkRoadTweeter.as I also have a class called User.as In my User class I need to call a method called nonce() I have tried this,
trace(SilkRoadTweeter(root).nonce());
But I get the error,
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Upvotes: 0
Views: 971
Reputation: 9572
Edit
I actually forgot one of the most important one, you could dispatch en event!
//In the Document class
var user:User = new User();
user.addEventListener( "nonce" , nonceEventListener );
//define your listener
private function nonceEventListener(event:Event):void
{
user.result = this.nonce();
}
//In the User class
public function result(value:Number):void
{
//here you deal the generated Number
}
//somewhere in the Class
this.dispatch( new Event("nonce") );
End of Edit
You should pass your instance of SilkRoadTweeter in the User class
For instance:
//In the Document class
var user:User = new User( this );
//In the User class
var srt:SilkRoadTweeter;
public function User( srt:SilkRoadTweeter )
{
this.srt = srt;
srt.nonce();
}
If the User class instance is added as a child of the SilkRoadTweeter class, you could also do this
//In the User class
var srt:SilkRoadTweeter;
public function User()
{
addEventListener(Event.ADDED , addedListener );
}
protected function addedListener(event:Event ):void
{
srt = this.parent as SilkRoadTweeter;
srt.nonce();
}
Upvotes: 1
Reputation: 1243
You said that the nonce() function is in the SilkRoadTweeter class and you need to call it from the User class. You must have a reference to the SilkRoadTweeter in order to call functions on it. The error is saying it doesn't know what a SilkRoadTweeter is. Using root to get a reference is messy in my opinion and should be avoided. I would suggest passing a reference to the User class when you create it. If that is not an option, making the function nonce() on the SilkRoadTweeter class static would solve your problem, so long as the function doesn't need to access non-static properties of the SilkRoadTweeter. You said all it does is return a generated number so I would guess it doesn't need to access non-static properties. I can elaborate further on what I've said if your still confused.
Upvotes: 1
Reputation: 2571
try:
trace(root is SilkRoadTweeter)
I have feeling you need to do:
trace((root.getChildAt(0) as SilkRoadTweeter).nonce());
Upvotes: 0