error #1009 can't access property of null object - actionscript-3

i'm newbie here i got an eror where i can't load external swf from my main fla the ouput said like this
`TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.senocular.utils::KeyObject/construct()
at com.senocular.utils::KeyObject()
at com.asgamer.basics1::Ship()
at com.asgamer.basics1::Engine()`
I create a button and tried to load swf file called basics1 which content all those classes above, and i using action code snippet to load swf file
please help i'm not a programmer sorry if my question in easy

the problem is fix by adding this
if(stage) {
initialize();
} else {
addEventListener(Event.ADDED_TO_STAGE,initialize);
}
}
private function initialize(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE,initialize);
// here goes the code that's currently in Engine constructor
}
after public function engine

Related

Flex 4.6 and AS3 Calling method from another mxml file

I have two files,
FloorplansMaster and Floorplans.
In my FloorplansMaster I have this method inside my script tags:
public function changeView(): void
{
var floorplans:Floorplans = new Floorplans();
floorplans.changeView();
}
and I am trying to call the method changeView in the Floorplans file, which looks like this:
public function changeView():void
{
trace("Here");
}
But I keep getting this error:
Cannot access a property or method of a null object reference.
What am I doing wrong?
In Flex developers often try to access view elements before they're created.
Try calling the method in a creationComplete handler instead.

addChild() issue: error 2007 on AS3

I have this piece of code that's suppose to add a swf file (homePage.swf) inside my main file (skeleton.fla).
Code:
var mcHome:MovieClip;
var newPage:Loader = new Loader();
newPage.load(new URLRequest("homePage.swf"));
newPage.contentLoaderInfo.addEventListener(Event.COMPLETE, homeLoaded);
function homeLoaded(event:Event):void {
mcHome = MovieClip(newPage.contentLoaderInfo.content);
newPage.contentLoaderInfo.removeEventListener(Event.COMPLETE, homeLoaded);
addChild(mcHome);
}
I keep getting this error:
TypeError: Error #2007: Parameter child must be non-null. at
flash.display::DisplayObjectContainer/addChild() at
skeleton_fla::MainTimeline/homeLoaded()
I don't know how to solve it, or what to change!
Help please, I'm a bit desperate.
Is better to add to the displayList the Loader object instead its contentLoaderInfo.content. The Loader is a DisplayObject by itself. There is not need to access to the MovieClip inside of the Loader object although it is possible in the most of environments.
If you try to load an SWF that resides in other domain, you could add the Loader object to the displayList but you can't access to the content property if you don't create a crossdomain.xml file.
var newPage:Loader = new Loader();
newPage.load(new URLRequest("homePage.swf"));
newPage.contentLoaderInfo.addEventListener(Event.COMPLETE, homeLoaded);
function homeLoaded(event:Event):void {
newPage.contentLoaderInfo.removeEventListener(Event.COMPLETE, homeLoaded);
addChild(newPage);
}
Here you have an example.

MovieClip(root) line seems to be crashing my game

Inside my classes, I invoke this function
MovieClip(root).increaseScore();
which handles the score in the main .as file.
It all works fine during the execution of the level. However when the level is finished and the screen goes to another frame, the game crashes and gives me this error
TypeError: Error #1009: Cannot access a property or method of a null
object reference.
on the line above.
How do I fix this?
Thanks
edit:
This is were I tell it to addScore, this is in the GameController.as file
private function removeBubble(bubble, addScore:Boolean)
{
var delay:Timer = new Timer(200, 1);
delay.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent)
{
if(bubble.parent==mcGameStage)
{
var j:int = bubbleList.indexOf(bubble);
bubbleList.splice(j,1);
if(addScore) bubble.addScore();
mcGameUI.txtScorePlayer.text = String(playerScore);
mcGameStage.removeChild(bubble);
}
e.currentTarget.removeEventListener(e.type, arguments.callee);
checkWin();
});
delay.start();
}
here is the checkWin function:
private function checkWin()
{
if (playerBlue + playerRed + playerYellow + playerOrange + playerPurple + playerGreen == 0)
{
gameWin();
}
}
private function gameWin()
{
while (bubbleList.numChildren > 0)
{
bubbleList.removeChildAt(0);
}
mcGameUI.btnMixBlue.removeEventListener(MouseEvent.CLICK, mixBlue);
mcGameUI.btnMixRed.removeEventListener(MouseEvent.CLICK, mixRed);
mcGameUI.btnMixYellow.removeEventListener(MouseEvent.CLICK, mixYellow);
mcGameUI.btnNeedle.removeEventListener(MouseEvent.CLICK, activateNeedle);
mcGameStage.removeEventListener(Event.ENTER_FRAME,update);
mcGameStage.removeEventListener(MouseEvent.CLICK, checkToHit);
removeEventListener(Event.ADDED_TO_STAGE, gameAddedToStage );
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
if (mouseCursor != null)
{
mouseCursor.removeEventListener(Event.ENTER_FRAME,followMouse);
mouseCursor = null;
}
gotoAndPlay("level1win");
}
And inside my classes,
public function addScore()
{
root["increaseScore"]();
}
This is what increaseScore does
public function increaseScore()
{
playerScore += 1000;
}
So where is the null object? D:
Also I am very inexperienced using the debugger so I apologize if this could be easily solved with that. I tried it and couldn't figure it out before coming here.
What is OPP method?
What is FrameScript?
Also, the class is MovieClip
thanks :)
Fixing this is done with the help of the debugger!
The error is telling you that something is null and you're trying to access it. That's not good, so let's think what could be null?
1) Your movie clip instance might be null.
2) increaseScore() method of your movie clip instance might try to access something that is null
You didn't post any code that I could analyse at the time I'm writing this answer, so I can't say for sure.
Some possible problems:
Your class is not called MovieClip, but you're just trying to cast your root object to a MovieClip. Thing is, MovieClip's don't have a increaseScore() method. You should instead call the increaseScore() method with
root["increaseScore"]();
This will call the method of your root timeline, but since we are using weak-typing, you might have problems with debugging it later. But I guess that's the price you pay when writing all the code in a frame instead of using OPP approach.
The true reason here is said in the error: Cannot access a property or method of a null object reference.
What this means is that MovieClip(root) is null (it doesn't even bother to check if the function is present). The reasons for this might be three:
Your document class is not MovieClip (could be Sprite).
The class you use root in, is not DisplayObject. The property is part of the DislpayObject class and so if you use it on other kind of classes that does not extend it, it won't work.
You are using this piece of code before you have added the instance to the stage (most probable cause). The root property represents the top-most display object in the tree. If you haven't added the child to the tree, it has no roots :) Check out parent - the error should be the same.

Flash Error #1009 on mouse click

I have a movie clip called AndroidEye2, which has 4 frames.
I have this code on my 'Main Menu' Scene:
function eye1(e){
AndroidEye2.gotoAndStop(2);
}
function standStill(e){
AndroidEye2.gotoAndStop(1);
}
ViewMerchandise_IconB.addEventListener(MouseEvent.CLICK,ViewMerchandise1);
function ViewMerchandise1(e) {
gotoAndPlay(1,"ViewMerchandise1");
}
ViewMerchandise_IconB.addEventListener(MouseEvent.MOUSE_OVER, eye1);
ViewMerchandise_IconB.addEventListener(MouseEvent.MOUSE_OUT, standStill);
I have this error
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at Tes_fla::MainTimeline/standStill()
Every time I try to click the ViewMerchandise_IconB button. What did I do wrong?
I'll send you the fla file if you needed to see the error.
The application is complaining about "AndroidEye2.gotoAndStop(1);" It says that AndroidEye2 (that specific instance or static class (depending on what it is)) does not have any property/function called "gotoAndStop". It could also be that AndroidEye2 is null/undefined when that function (standStill) is called.
Edit1:
standStill(e){
trace("instance=" + AndroidEye2);
trace("function=" + AndroidEye2.gotoAndStop);
AndroidEye2.gotoAndStop(1);
}

External SWF Loading problem

I have an SWF loading in an SWF containing a papervision scene.
I've done it before yet problem is, I get an error - I'm not sure what the issue really is.
private function downloadSWF(url:String):void
{
trace(url);
var urlRequest:URLRequest = new URLRequest(url);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loaderProgressEventHandler);
loader.load(urlRequest);
}
private function loaderProgressEventHandler(ev:ProgressEvent):void
{
loader.preloaderCircle.percent = ev.bytesLoaded / ev.bytesTotal;
}
When the application runs the code - I get the error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.dehash.pv3d.examples.physics::WowDemo()
Why am I getting this if the loading hasn't even complete yet?
Thanks in advance guys.
Edit: Try a blank child swf, see if the other one was trying access something in the parent. – Jorge
I did this, it seems, even with a simple SWF with a mouse click listener causes the Error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at simple_fla::MainTimeline/frame1()
My code for that is:
import flash.events.MouseEvent;
this.stage.addEventListener(MouseEvent.CLICK, onClick);
function onClick(ev:MouseEvent):void
{
trace("MouseClick");
}
Am I missing something blatantly obvious??
The problem is that the loaded swf starts running without it being added to the stage. So stage is null, resulting in that error.
The second example with the addedToStageEventHandler works because there stage is only referenced after the object was added to the stage, so stage is not null anymore.
A possible solution for the first error is adding the loader to the stage. That way, when the swf is loaded and starts, it already has a stage reference.
It won't even load if there's an error. You're accessing an unreferenced object on the WowDemo() class...did you instantiate correctly the class?
this seems to work inside my child SWF:
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageEventHandler);
function onClick(ev:MouseEvent):void
{
trace("MouseClick");
var event:Event = new Event("END", true, false);
this.dispatchEvent(event);
}
function addedToStageEventHandler(ev:Event):void
{
this.stage.addEventListener(MouseEvent.CLICK, onClick);
}
does anyone know Why?