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:
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!
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;
}
source to share