actionscript-3flash-cs6

AS3 -Error #1009: Cannot access a property or method of a null object reference


I'm in the process of creating an app, all the pages are loaded in using the Loader class and while my metro.as loads fine, when I try to load,

'chords.swf' via URLRequest (var chordReq:URLRequest) i get this error: TypeError: Error #1009: Cannot access a property or method of a null object reference. at Chords()

package  {

import flash.display.MovieClip;
import com.greensock.TweenMax;
import com.greensock.easing.*;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Loader;
import flash.net.URLRequest;



public class Main extends MovieClip {
    var chordsIcon:navBTN;
    var clockIcon:navBTN;
    var metroIcon:navBTN;

    var logo:logoMC;
    var _back:backBTN;

    var metroLoader:Loader;
    var metroReq:URLRequest;

    var chordLoader:Loader;
    var chordReq:URLRequest;



    public function Main() {
        // constructor code
        if(stage){
            initMain();
        }else{
             this.addEventListener(Event.ADDED_TO_STAGE, initMain);
        }

        function initMain(){
            chordsIcon = new navBTN('Chord Charts','Chords');
            metroIcon = new navBTN('Metronome','Metronome');
            clockIcon = new navBTN('Session Timer','Clock');
            logo = new logoMC;
            _back = new backBTN;

            chordLoader = new Loader();
            chordReq = new URLRequest('chords.swf');



            metroLoader = new Loader();
            metroReq = new URLRequest('metro.swf');

            addChild(chordLoader);
            chordLoader.x = 0;
            chordLoader.y = 0;



            addChild(metroLoader);
            metroLoader.x = 320;
            metroLoader.y = 0;
            metroLoader.load(metroReq);

            initInterface();

        }//end initMain

    function initInterface(){

        addChild(logo);
        logo.x = 160;
        logo.y = stage.stageHeight - (logo.height / 2);

        addChild(chordsIcon);
        chordsIcon.x = stage.stageWidth / 2;
        chordsIcon.y = 234;


        addChild(clockIcon);
        clockIcon.x = 90;
        clockIcon.y = 350;


        addChild(metroIcon);
        metroIcon.x = 230;
        metroIcon.y = 350;

        chordsIcon.addEventListener(MouseEvent.CLICK, onClick);
        clockIcon.addEventListener(MouseEvent.CLICK, onClick);
        metroIcon.addEventListener(MouseEvent.CLICK, onClick);





    }
    function removeInterface(){
        removeChild(logo);
        removeChild(clockIcon);
        removeChild(chordsIcon);
        removeChild(metroIcon);
    }

    function onClick(e:MouseEvent){
            removeInterface();

            addChild(_back);
            _back.x = 30;
            _back.y = 22;
            _back.addEventListener(MouseEvent.CLICK, onBackClick);


                if(e.target == chordsIcon.hitSpot){
                    trace('Chords Clicked');

                    chordLoader.load(chordReq);

                }
                else if(e.target == metroIcon.hitSpot){
                    trace('Metronome Clicked');
                    metroLoader.x = 0;

                } 
                else if(e.target == clockIcon.hitSpot){
                    trace('Clock Clicked');

                }




        }

        function onBackClick(e:MouseEvent){
            metroLoader.x = 320;
            initInterface();
        }
    }//end public function
}

}

not sure why this isn't working because metroLoader loads fine, while the other won't go.

chords.as looks like this...

package  {

import flash.display.MovieClip;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.events.Event;
import com.greensock.TweenMax;
import com.greensock.easing.*;


public class Chords extends MovieClip {
    //declare vars


    var cMaj:MovieClip  = new cMaj_MC;
    var cMin:MovieClip  = new cMin_MC;
    var c7:MovieClip    = new c7_MC;
    var dMaj:MovieClip  = new dMaj_MC;
    var dMin:MovieClip  = new dMin_MC;
    var d7:MovieClip    = new d7_MC;
    var eMaj:MovieClip  = new eMaj_MC;
    var eMin:MovieClip  = new eMin_MC;
    var e7:MovieClip    = new e7_MC;
    var fMaj:MovieClip  = new fMaj_MC;
    var fMin:MovieClip  = new fMin_MC;
    var f7 :MovieClip   = new f7_MC;

    var slideRight:rightBTN;
    var slideLeft:leftBTN;

    var chordList:Array = [cMaj, cMin, c7, dMaj, dMin, d7, eMaj, eMin, e7, fMaj, fMin, f7];
    var currentMC = chordList[i];
    var i:int = 0;


    var verticalCenter = (stage.stageHeight / 2) - (currentMC.height / 2);
    var horizontalCenter = (stage.stageWidth / 2) - (currentMC.width / 2);

    public function Chords() {
        // constructor code
        if(stage){
            initChords();
        }else{
             this.addEventListener(Event.ADDED_TO_STAGE, initChords);
        }


        function initChords() {
            slideRight = new rightBTN;
            slideLeft = new leftBTN;

            addChild(slideRight);
            slideRight.x = stage.stageWidth - (slideRight.width);

            addChild(slideLeft);
            slideLeft.x = 0;

            slideRight.addEventListener(MouseEvent.CLICK, onRight);
            slideLeft.addEventListener(MouseEvent.CLICK, onLeft);
            slideRight.alpha = .5;
            slideLeft.alpha = .5;

            addChild(currentMC);
            currentMC.x = horizontalCenter;
            currentMC.y = verticalCenter;

        }//end initChords


        function onRight(e:MouseEvent){
                trace(i);
                trace(currentMC);



                if(i == 11) {
                    slideRight.removeEventListener(MouseEvent.CLICK, onRight);

                }else{
                    slideRight.addEventListener(MouseEvent.CLICK, onRight);
                    TweenMax.to(slideRight, .05, {alpha:1, yoyo:true, repeat:1});
                    TweenMax.to(currentMC, .25, {x:-320, alpha:0, onComplete:nextChord});
                }


        }
        function nextChord(){

            removeChild(currentMC);

            i++;

            addChild(currentMC);
            currentMC.x = 320;
            currentMC.y = verticalCenter;
            currentMC.alpha = 0;


            TweenMax.to(currentMC, .25, {x:horizontalCenter, alpha:1});

        }
        function lastChord(){

            removeChild(currentMC);

            i--;

            addChild(currentMC);
            currentMC.x = -320;
            currentMC.y = verticalCenter;
            currentMC.alpha = 0;

            TweenMax.to(currentMC, .25, {x:horizontalCenter, alpha:1});

        }

        function onLeft(e:MouseEvent){

            if(i > 0) {
                slideLeft.addEventListener(MouseEvent.CLICK, onLeft);
                TweenMax.to(slideLeft, .05, {alpha:1, yoyo:true, repeat:1});
                TweenMax.to(currentMC, .25, {x:320, alpha:0, onComplete:lastChord});
            }

        }

        }//end public function


}//end public class

}//end package

Solution

  • Have the declaraion of the variables verticalCenter , horizontalCenter on top and move these lines into function initChords

    verticalCenter = (stage.stageHeight / 2) - (currentMC.height / 2);
    horizontalCenter = (stage.stageWidth / 2) - (currentMC.width / 2);
    

    Hope it helps.