Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Dec 03, 2016 Dec 03, 2016

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);

  }

  }

}

TOPICS
ActionScript
556
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 03, 2016 Dec 03, 2016

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

Translate
Community Expert ,
Dec 03, 2016 Dec 03, 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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 03, 2016 Dec 03, 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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 03, 2016 Dec 03, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 03, 2016 Dec 03, 2016

Thank you for helping

Finally managed to solve the problem.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 04, 2016 Dec 04, 2016
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines