Skip to main content
Participant
December 3, 2016
Answered

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

Hello

I am currently producing an ECard for my school assignment but I kept receiving this error at the output box.

When I click on my wood movie clip after a number of times in the Campfire class,it's suppose to change visibility of the sad/Happy Protag of the Protagonist class but the error kept popping up. Anyway to solve this problem?

Campfire class:

package {

  import flash.display.*;

  import flash.events.*;

  import flash.sampler.NewObjectSample;

  public class Campfire extends MovieClip {

  // Properties

  private var fire: Fire;

  private var wood: Wood;

  private var spark: Spark;

  private var sparks: Array = new Array;

  private var sparksLeft: int;

  private var protagonist: Protagonist;

  // Constructor

  public function Campfire() {

  sparksLeft = 3;

  this.addEventListener(MouseEvent.CLICK, activateSpark);

  this.addEventListener(MouseEvent.CLICK, activateFire);

  this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

  }

  // Methods

  // Method: Create wood onto stage

  private function addedToStageHandler(evt: Event): void {

  wood = new Wood();

  wood.x = 170;

  wood.y = 150;

  addChild(wood);

  }

  // Method: Create spark onto stage

  public function activateSpark(evt: MouseEvent): void {

  var i: int = 0;

  spark = new Spark();

  spark.x = (Math.random() * 50) + 200;

  spark.y = 140;

  addChild(spark);

  sparks.push(spark);

  sparksLeft = sparksLeft - 1;

  }

  // Method: Setter method for campfire

  public function setProtagonist(iProtagonist): void {

  protagonist = iProtagonist;

  }

  public function activateFire(evt: MouseEvent): void {

  if (sparksLeft == 0) {

  fire = new Fire();

  fire.x = 200;

  fire.y = 132;

  addChild(fire)

  this.removeEventListener(MouseEvent.CLICK, activateSpark);

  this.removeEventListener(MouseEvent.CLICK, activateFire);

  protagonist.changeToHappyProtag();

  }

  }

  }

}

Protagonist Class

package {

  import flash.events.Event;

  import flash.display.MovieClip;

  public class Protagonist extends MovieClip {

  // Properties

  private var happyPtg: HappyProtag;

  private var sadPtg: SadProtag;

  // Constructor

  public function Protagonist() {

  this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

  }

  // Methods

  // Method: Create sad protagonist

  private function addedToStageHandler(evt: Event): void {

  sadPtg = new SadProtag();

  sadPtg.x = 185;

  sadPtg.y = 80;

  addChild(sadPtg);

  happyPtg = new HappyProtag();

  happyPtg.x = 185;

  happyPtg.y = 80;

  addChild(happyPtg);

  sadPtg.visible = true;

  happyPtg.visible = false;

  }

  // Method: Change into happy protagonist

  public function changeToHappyProtag() {

  sadPtg.visible = false;

  happyPtg.visible = true;

  }

  }

}

Main Class

package  {

  import flash.display.*;

  import flash.events.*;

  public class Main extends MovieClip {

  // Properties

  private var campfire:Campfire;

  private var protagonist:Protagonist;

  // Constructor

  public function Main() {

  // Create objects onto stage

  this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

  }

  // Methods

  // Method: Creating objects

  private function addedToStageHandler(evt:Event):void {

  // Create protagonist

  protagonist = new Protagonist();

  protagonist.x = 0;

  protagonist.y = 0;

  addChild(protagonist)

  // Create campfire

  campfire = new Campfire();

  campfire.x = 0;

  campfire.y = 0;

  addChild(campfire);

  }

  }

}

This topic is closed to new replies. Start a new post to keep the conversation going.
kglad
kgladCorrect answer
Community Expert
December 4, 2016

protagonist doesn't exist when that line of code executes and the error message appears.

5 replies

kglad
Community Expert
Community Expert
December 3, 2016

click file>publish settings>swf and tick 'permit debugging'.

retest.

the problematic line number will be in the error message.  that line of code tries to reference an object that doesn't exist when the code executes.

Translate
Participant
December 4, 2016

It says that the problematic line was:

'  protagonist.changeToHappyProtag(); '

Im not exactly sure how to solve this problem though.

Would love some help with the code.

Translate
kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
December 4, 2016

protagonist doesn't exist when that line of code executes and the error message appears.

Translate