Reference - Null Object error when using stage.addEventListener (ScrollBar)

Hey guys, hoping for some help on this :( stuck on it for a couple of days.

I am creating a ScrollBar using Lee Bremelow's ScrollBar class . I had to change it to work inside my class files and think that I was on the right track, but I get fear:

Bug # 1009: Cannot access property or method of null reference error .

When I run debug it hits the line where I have rollUp);

CLASS VIEWED CODE:

import flash.display.*;
import flash.events.*;
import caurina.transitions.*;

public class ScrollBar extends MovieClip
{
    private var yOffset:Number;
    private var yMin:Number;
    private var yMax:Number;
    private var thumbsnailTab:MovieClip;
    private var theRoller:MovieClip;

    public function ScrollBar(myRoller:MovieClip, myTrack:MovieClip, thumbsnails:MovieClip):void
    {
        yMin = 0;
        yMax = myTrack.height - myRoller.height;
        theRoller = myRoller;
        thumbsnailTab = thumbsnails;
        myRoller.addEventListener(MouseEvent.MOUSE_DOWN, rollerDown);
        stage.addEventListener(MouseEvent.MOUSE_UP, rollerUp);
    }

      

This is what my debug shows:

alt text

At first I wasn't sure if this stage reference was causing the error or the rollUp function, but since I commented out the stage.removeEventListener and added a basic trace statement, it still throws an error, so I believe it has something to do with:

stage.addEventListener(MouseEvent.MOUSE_UP, rollerUp);

      

Now I have imported events. *; to the ScrollBar class ... maybe the problem is in my main class when I create graphics for the ScrollBar and also add the ScrollBar to the display list?

CODE FROM MAIN CLASS:

// Creating Graphics
        track1 = new Track;
        track1.x = 0;
        track1.y = 0;

        roller1 = new Roller;
        roller1.x = 0;
        roller1.y = 0;

        sc1 = new EmptyMov;
        sc1.x = 764;
        sc1.y = 470;

        sc1.addChild(track1);
        sc1.addChild(roller1);

// Adding ScrollBar to Stage
scroll1 = new ScrollBar(roller1, track1, tab1);
container.addChild(sc1);
container.addChild(scroll1);
addChild(container);

      

I'm stuck here, not sure why I am getting this null reference error, and also not sure if I am rendering the graphics correctly, and also using the ScrollBar class correctly :( any hints are appreciated!

alt text


Update work code !: D

public function ScrollBar(myRoller:MovieClip, myTrack:MovieClip, thumbsnails:MovieClip):void
    {
        yMin = 0;
        yMax = myTrack.height - myRoller.height;
        theRoller = myRoller;
        thumbsnailTab = thumbsnails;
        myRoller.addEventListener(MouseEvent.MOUSE_DOWN, rollerDown);
    }

    private function rollerDown(e:MouseEvent):void
    {
        stage.addEventListener(MouseEvent.MOUSE_UP, rollerUp);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, rollerMove);
        yOffset = mouseY - theRoller.y;
    }

      

alt text

+2


source to share


3 answers


I think your problem is stage = null. There can be only one reason: you are trying to jump to the scene when the MC is not added to it (then the link is zero).



+3


source


Your stage is null because the newly created object is not on stage yet. To work around this, use the ADDED_TO_STAGE listener in the constructor, which then adds your stage events.



+5


source


I agree with Konrad, also .. Better to start triggering the MOUSE_UP event when the MOUSE_DOWN event is captured.

move

stage.addEventListener(MouseEvent.MOUSE_UP, rollerUp);

      

inside the rollDown function.

+2


source







All Articles