AS3: How do I start a parent's timer? - actionscript-3

I have a function in a child that is supposed to start the timer in the parent class. The code to do so is:
MovieClip(this.parent).Investment1Timer.start();
But when it tries to do that, I get this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
I can access the parent's functions and variables just fine, but I can't start any of its timers.

It's likely a scope issue, and Investment1Timer in the parent is private.
It should be :
public var Investment1Timer:Timer;
If you don't specify public or private, it will default to private and will not be accessible outside of that class code.
You should also verify that this.parent is not null and is actually a MovieClip or inherits from MovieClip.
I find the best way to investigate this kind of issue is by running in debug mode as you can then just check the variables involved to pinpoint what is null. Debugger is your friend!
I should also note that it's possible that you have declared Investment1Timer, but you did not create the instance, therefore it is still null ?

Related

AS3 MVC Event Listener [Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference

I am using the MVC pattern as described by
http://www.swinburne.edu.au/design/tutorials/P-flash/T-The-Model-View-Controller-Design-Pattern-in-Actionscript-3/ID-144/
However for my project I am using a view with child sprites like this in ascii art:
[[menu][ Content ]]
the container for content is called displayPanelContent
The content sprite uses a listener like below to use the update function:
super.getModel().addEventListener(Event.CHANGE, this.update, false, 0, true);
however when I try and delete the content sprite using
while (displayPanelContent.numChildren > 1) {
displayPanelContent.removeChildAt(1);
}
I get an error message:
[Fault] exception, information=TypeError: Error #1009: Cannot access a
property or method of a null object reference.
If I remove the listener it deletes fine but the error message is so vague I can't discern the error.
I'm trying to avoid posting the code because the whole pattern and application is really verbose.
It's not vague at all.
It clearly states that you are operating on an object reference that doesn't reference an object, but null.
So whatever line causes the error includes a reference that's referencing null.

in what situation,gotoAndStop is not working

I have 3 frames, I don't show the whole code, it's too huge.and the main code is
gotoAndStop(2);
trace('frame:',currentFrame)
the output should be frame: 2
But in fact it's frame: 1, and objects in frame 2 cannot be loaded and become null
no compiler errors
When I delete some codes after it, the application sometimes operate right and stops at frame 2.
This shouldn't happen as any code after it should not involve the output
although I can solve this when I delete the first frame,but it's quite risky to keep developing.
Any ideas why this happen?
Before keep going,I should mention I actually have compiler errors due to null objects,but it's not the main point.
And I have a class called host,the code gotoAndStop is also in the first place of constructor.
I have put override function in host, and the output is
stopping at frame: 2 Called from: Error
at host/gotoAndStop()
at host()
frame: 1
TypeError: Error #1009: Cannot access a property or method of a null object reference
at host()
Then I tried the method2 Creative Magic says,the result shows
stopping at frame: 2 Called from: Error
at host/gotoAndStop()
at host()
displaying frame: 1
frame: 1
TypeError: Error #1009: Cannot access a property or method of a null object reference
at host()
displaying frame: 2
This quite confuses me what's happening in the frame,I wonder the cause is like you say old version of SDK,thanks for help
Your problem can be either you've written buggy code or you use old Flash SDK.
In the first case you should check if you're calling gotoAndStop(), play() or gotoAndPlay() methods of your MovieClip. To do so you can edit the class of your MC:
In Flash Professional in the library panel right-click on the MC
Select "Edit Class"
In your selected IDE add following function
override public function gotoAndStop(frame:Object, scene:String = null):void
{
trace("stopping at frame:", frame, "Called from:", new Error().getStackTrace());
super(this).gotoAndStop(frame, scene);
}
It will trace when the gotoAndStop() method was called and who called it. It will let you see if the frame was set to frame by something else. I recommend you to override play() and gotoAndPlay() functions as well.
If you're intimidated by this code you could just add a trace() on each frame of your MC, since it's only 3 frames it shouldn't be too much work. Your trace could look something like that:
trace("displaying frame:", this.currentFrame);
The other possible cause could lie within the SDK you're using. I, myself had a strange bug when the loaded MC wouldn't listen to stop(), gotoAndStop() and other functions. The problem was solved by upgrading the SDK: http://www.adobe.com/devnet/flex/flex-sdk-download.html
The explanation on how to replace the old SDK is there as well.

TypeError: Error #1009: Cannot access a property or method of a null object reference at pizzaBaking/doRESTART()

I have been working in this error for days, i keep searching and asking for help, (including reading from this site as well). It is basically involved a lot of external actionscript.
Every time i finish the interactive and i want to go back to previous frame, the error goes:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at pizzaBaking/doRESTART()
and this is the doRESTART function
public function doRESTART(e:MouseEvent):void{
MovieClip(parent).gotoAndPlay(131);
MovieClip(parent).MENUS.visible = false;
MovieClip(parent).ABOUT.visible = false;
MovieClip(parent).VOUCHER.visible = false;
MovieClip(parent).UPDATES.visible = false;
Due to quite a few of file, i put it all in mediafire, in flash cs6 http://www.mediafire.com/download/10skpaizeyk2kul/pizzaHut.rar
thank you so much
1: Turn on Debugging
Go to your publish settings and enable "Permit Debugging". This will take your ambiguous error...
null object reference at pizzaBaking/doRESTART()
...to a much clearer...
null object reference at pizzaBaking/doRESTART() script\pizzaBaking.as:35
...which now denotes which line in your code to look for your issue.
2: Use the Debug Console
Use the debugging compile mode to bring up the Debug Console. This will provide you with an immediate look at the line of code in question, as well as the Call Stack, and the state of all available Variables. No programmer should be without one.
3: parent == null
When doRESTART() is being called, it no longer has a parent, ergo, when you call MovieClip(parent) it returns a null, and there is no MENUS property on null. This is why it's reporting null object reference.
If I were you, I'd seriously reconsider how you're approaching this. Remove all timeline code, and any use of frame animation. This is likely the reason why your pizzaBaking class lost its parent relationship. Also, there appear to be several instances in which you could consolidate repetitious code into a single function (KeyboardPizza.as being a prime example). Consider passing a method an argument to modify its behavior, or use switch case, or perhaps a lookup table.
That said, press on. It looks like you're making great progress on your project.
-Cheers

Flex mobile - Spark Chechbox Error

I'm using Flex SDK 4.6, very strange error occurs when i try to add spark checkbox on the stage:
var cb:CheckBox = new CheckBox();
optionsList.addElement(cb);
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
spark.skins.mobile::CheckBoxSkin/drawBackground()[E:\dev\4.y\frameworks\projects\mobiletheme\src\spark\skins\mobile\CheckBoxSkin.as:162]
at
spark.skins.mobile.supportClasses::MobileSkin/updateDisplayList()[E:\dev\4.y\frameworks\projects\mobiletheme\src\spark\skins\mobile\supportClasses\MobileSkin.as:313]
at
mx.core::UIComponent/validateDisplayList()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:8999]
at
mx.managers::LayoutManager/validateDisplayList()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\LayoutManager.as:736]
at
mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\LayoutManager.as:819]
at
mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1180]
When the chechbox is created in MXML there are no problems, but I need to create multiple instances in AS and than add them to stage.
Anyone have any clue what I'm doing wrong?
I'm posting to clear up a misconception, which was in the comments, and I believe is the root of the error. To quote:
I checked when I add the checkbox on creationComplete it works, but in
my case the checkboxes are added AFTER creationComplete when
updateDisplayList is called.
updateDisplayList() is called, potentially, a lot during a component's lifecycle. It is called every time something on the screen needs to update itself. updateDisplayList() is called at least once before the creationComplete event is dispatched, however in some situations, with some components, it could be called more times.
The code you shared is taken a bit out of context, however as it is you will end up with a lot of checkboxes; because a new checkbox will be created after every iteration of updateDisplayList(). I strongly recommend you move your children creation to the createChildren() method instead of updateDisplayList().
Here is some documentation on the Spark Flex Component LifeCycle and some on MX Component LifeCycle.

Calling MovieClip(root) from a dynamic MovieClip instance produces error 1034

NINJA EDIT:
For some reason, the same code works now, without any problem at all. I don't know what happened, or why, but I no longer have this problem
Here's the original post:
To put simply, I created a MovieClip, put it with addChild() to stage, and when I tried to call this piece of code:
MovieClip(root).someFunction();
It throws Error #1034: Type Coercion failed: cannot convert flash.display::Stage#4034f71 to flash.display.MovieClip.
I really can't figure out why this piece of code won't work. The object itself works perfectly, as I can call functions within it (that line of code is actually within a function). It's just that piece of code that is problematic
Can someone tell me where I went wrong?
EDIT:
To better illustrate the situation, here's my pieces of code:
in a MovieClip, I have this function:
function bombReset():void
{
bBombIsDropped = false;
tCarpetBombTween.gotoAndStop(0);
this.visible = false;
MovieClip(root).carpetBombAttack(iPosition);
}
And on Scene1(root, the outermost parent) I have this function:
function carpetBombAttack(position:int):void
{
damagePlant(15,vTileOccupant[(position-1)]);
}
If I create a MovieClip instance via addChild and call bombReset in it, Flash will throw an error
If I manually drag the MovieClip onto stage, when I call bombReset, it will work fine
Your error means that the compiler doesn't know how a MovieClip and a Stage can be the same thing. Also, I'm not certain, but I believe the compiler will whine about someFunction not existing on the stage even if you casted the stage (aka root) correctly.
The proper way to solve this is by assigning a document class to your project and make someFunction a public method (class function).
The lay-mans solution (which I sometimes use when I'm being lazy) is the following
Object(this.stage).someFunction();
That works because you are type-casting this.stage in a way that makes the compiler think it's an Object instead of a Stage. Objects can have any number of undocumented properties and functions, thus allowing you to call items on the Object whether they are part of a class definition or not (and even ones that don't exist - which is where you can get yourself into trouble).
The inheritance for Stage is Stage -> DisplayObjectContainer -> InteractiveObject -> ... while MovieClip is MovieClip -> Sprite -> DisplayObjectContainer -> InteractiveObject -> ... (I'd link directly to the docs but the pages keep crashing on me).
While they share common base classes, the Stage and MovieClip classes aren't actually related, so trying to cast one as the other will fail.
As you're doing the MovieClip(root) type cast and not the root as MovieClip cast, that's why you're getting the error you're getting.
Either cast it directly to the object that has the someFunction() defined, use the solution defined by Jackson, or if you absolutely know it's there, you can also do root["someFunction"]()