AS3: Cannot access a property of a null object reference - actionscript-3

I'm trying to trace the mouse X position with a movieclip.
What I have done so far:
public class LuckyHitBeta extends MovieClip {
var ballReady:ballReady_mc;
private function liveIcon():void {
ballReady=new ballReady_mc();
addChild(ballReady);
ballReady.y=1;
}
private function onEnterFrm(e:Event):void
{
ballReady.x=mouseX;
}
}
Runtime error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at LuckyHitBeta/onEnterFrm()

I don't see you calling the function liveIcon(). It seems like the enterFrame event happens before ballready is initialized.

I think it means ballReady is NULL
I would do something like
private function onEnterFrm(e:Event):void
{
if (ballReady) {
ballReady.x=mouseX;
}
}

Related

TRIED ALMOST EVERYTHING: ypeError: Error #1009: Cannot access a property or method of a null object reference

I am having this problem and cant get around it at all
im making a platform game for uni
i have 2 files
flashgame.fla and Coin.as (this is code for the Coin class)
i have code stating that once the player has collected all the coins the frame will change from frame 1 to frame 2.
When i do i receive this message
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at Coin/update() TypeError: Error #1009: Cannot
access a property or method of a null object reference. at
flashgame_fla::MainTimeline/loop()
i have tried Try and Catch and various other things
I think it does this because the Coin.as extends MovieClip so when it goes to the next frame it is still trying to find a coin when nothing is there.
here is the code for Coin.as
package {
import flash.display.MovieClip;
import flash.events.*;
public class Coin extends MovieClip {
var player:MovieClip;
var mainTimeLine = MovieClip(root);
public function Coin() {
this.addEventListener(Event.ENTER_FRAME, update);
}
function update(event:Event):void
{
player=MovieClip(root).player;
if(this.hitTestObject(player))
{
this.removeEventListener(Event.ENTER_FRAME, update);
parent.removeChild(this);
mainTimeLine.coinCount++;
}
}
}
}
i have an array in flashgame.fla that records all coins in the game. when the player hits them they are spliced from the array. could also be causing the problem when going to frame 2
important stuff from flashgame.fla
var coin:Array = new Array();
for (i=0; i<numChildren; i++)
{
if (getChildAt(i) is Coin)
{
coin.push(getChildAt(i).getRect(this));
}
}
splicing coins
for (i=0; i<coin.length; i++)
{
if (player.getRect(this).intersects(coin[i]))
{
coinSnd.play();
coin.splice(i,1);
}
}
Thanks for any help given
if you need anything more from me please ask :)
all g with screenshots
It's most likely because the player cannot be found in the update function in the Coin class. This is because ENTER_FRAME events are run even if the display object is not on the stage, which you often don't want. In this case you try to do a hittest against the player.
To solve this, you can start running update as soon as the Coin is attached to the stage and stop running it as soon as it's removed from the stage.
public function Coin() {
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
function onAddedToStage(event:Event):void {
this.addEventListener(Event.ENTER_FRAME, update);
}
function onRemovedFromStage(event:Event):void {
this.removeEventListener(Event.ENTER_FRAME, update);
}
Which line number is the error occurring on? That should tell you which variable is null.
My guess, based on this part of the error: "TypeError: Error #1009: Cannot access a property or method of a null object reference. at Coin/update()", is that the parent of the coin is null because you've moved to a new keyframe and the instance has been removed from the stage.
That is, I suspect the error is occurring on the line with parent.removeChild(this); - is this correct?
If that's the case then check whether parent is not null before calling removeChild on it.

MovieClip does everything it's supposed to, and still there is `error 1009`

I'm creating game in which user is given a map and five flags. User must drag flags, one by one to the countries, so after creating MovieClips that represent flags, I started creating code for them. That worked for France, but after I copied it to Germany and changed everything using CTRL+F ('france' to 'germany' and 'France' to 'Germany') it didn't. Well, actually it did, but there is some error. That's funny, coz German flag does absolutely everything it should, and still there is an error in the output. Here, that's the code:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class flagGermany extends MovieClip {
public var Name:String="Germany";
public var atX:uint;
public var atY:uint;
public var wasGuessed:Boolean=false;
public var isPressed:Boolean=false;
public function flagGermany() {
trace(this);
this.addEventListener(MouseEvent.MOUSE_DOWN, dragEnable);
this.addEventListener(MouseEvent.MOUSE_UP, dragDisable);
stage.addEventListener(MouseEvent.MOUSE_UP, dragDisable);
}
function dragEnable(e:MouseEvent) {
isPressed=true;
trace(isPressed);
atX=stage.mouseX-this.x;
atY=stage.mouseY-this.y;
this.alpha=0.3;
this.mouseEnabled=false;
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveFlag);
}
function dragDisable(e:MouseEvent) {
if(isPressed==true) {
if(wasGuessed==false) {
this.alpha=1;
this.mouseEnabled=true;
}
trace("invoked by "+e.target);
if(MovieClip(this.root).germany.currentFrame==2) {
MovieClip(this.root).germany.gotoAndStop(3);
MovieClip(this.root).germany.removeEventListener(MouseEvent.ROLL_OVER, MovieClip(this.root).hightlight);
MovieClip(this.root).germany.removeEventListener(MouseEvent.ROLL_OUT, MovieClip(this.root).unhightlight);
correct();
}
stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveFlag);
isPressed=false;
trace(isPressed);
}
}
function moveFlag(e:MouseEvent) {
this.x=stage.mouseX-atX;
this.y=stage.mouseY-atY;
}
function correct() {
this.removeEventListener(MouseEvent.MOUSE_DOWN, dragEnable);
this.removeEventListener(MouseEvent.MOUSE_UP, dragDisable);
this.alpha=0;
this.mouseEnabled=false;
this.wasGuessed=true;
}
}
}
And here's an error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at flagGermany()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at EM()
EM is main file's Document Class. flagGermany() a constructor for German flag. Both flags are created on second frame of the movie. What's the matter with my code?!
I believe for stage to non-null, the MovieClip has to be addChild'ed first. I would check out why is France getting stage and Germany not.
Make sure stage is available before adding event listeners to it:
public function flagGermany() {
/* .. */
addEventListener("addedToStage", onAddedToStage);
}
private function onAddedToStage(event:*):void {
stage.addEventListener(MouseEvent.MOUSE_UP, dragDisable);
}

AS3 ENTER_FRAME event still firing when frame changed

Why won't this ENTER_FRAME event stop firing when I call view_stats_exit before going to view_start?
public function view_start (e:MouseEvent):void
{
gotoAndStop("start");
}
public function view_stats(e:MouseEvent):void
{
// Event
StatsUI.addEventListener(Event.ENTER_FRAME,stats_scroll);
}
public function view_stats_exit (e:MouseEvent):void
{
StatsUI.removeEventListener(Event.ENTER_FRAME,stats_scroll);
view_start(null);
}
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Snapshot/stats_scroll()
You can add an event listener to a MovieClip , but you shouldn't do it as a static function , like in your example. The following should work...
private var ui:StatsUI = new StatsUI();
public function view_stats(e:MouseEvent):void
{
// Event
ui.addEventListener(Event.ENTER_FRAME,stats_scroll);
}
public function view_stats_exit (e:MouseEvent):void
{
ui.removeEventListener(Event.ENTER_FRAME,stats_scroll);
view_start(null);
}

Accessing main class stage event listener

I wanted to remove an event listener from main class stage, but i get the error 1120: Access of undefined property stage. How do I actually access the stage?
custom class:
import main;
main.disableVcam();
main class:
public static function disableVcam():void {
trace("disable");
stage.removeEventListener(MouseEvent.MOUSE_MOVE, movevC);
}
Unless the object is on the display stage, the stage object will be undefined (or null). You have to addChild the object for the stage object to have a value.
Edit: Perhaps you can handle this in the event handler?
protected function clickHandler(e :Event) :void {
if (e.target.stage) {
e.target.stage.removeEventListener(...);
}
}
Edit2: Static methods don't have a stage, so to solve your problem you can make your Main-class a singleton, and work like this:
public class Main {
static private var instance :Main;
static public function getInstance() :Main {
if (Main.instance == undefined) {
Main.instance = new Main();
}
return Main.instance;
}
// The rest of the class goes here
}
// snip
import Main;
public static function disableVcam():void {
trace("disable");
Main.getInstance().stage.removeEventListener(MouseEvent.MOUSE_MOVE, movevC);
}
If your Main-class is the main class of the project, you need to assign the static instance variable's value in the constructor.

when does the stage become initialised?

I have a singleton class that inherits from sprite so that it can access the stage, like this..
package
{
import flash.display.Sprite;
public class C extends Sprite
{
private var _grid:Array = new Array();
public function get Grid():Array
{
return _grid;
}
private static var _instance:C;
public static function get Instance():C
{
if (_instance == null)
{
_instance = new C();
}
return _instance;
}
function C()
{
this.InitGrid();
}
private function InitGrid():void
{
var gridWidth:Number = stage.width / 10;
}
}
}
This throws the error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at C/InitGrid()
at C()
at C$/get Instance()
at C()
at Main()
If I replace stage.width with an int the code executes OK.
is this because the object has not been added to the displayList of any children of the stage?
Thanks
Yes. The Sprite will only have a stage property once it's a part of the Display list.
To get the stage you will need to either give your singleton a reference to the stage or add it to the Display list. If you choose the latter you can add a listener Event.ADDED_TO_STAGE, and handle that accordingly inside your singleton.