Skip to main content
Participating Frequently
November 18, 2009
Question

error #1009 while loading swf "Cannot access a property or method of a null object reference."

  • November 18, 2009
  • 2 replies
  • 810 views

Hello

I'm trying to load a swf called "polaroids.swf" into my main swf called "09replacesSWF.swf". I keep getting the error when I test the movie. I'm completely lost and have been at this for hours. If I just test polaroids.fla the movie works fine but if I try to load it into 09replacesSWF.swf, I get the error. I need some help PLEASE!!!!!

I tried to debug the movie and flash says......."Cannot display source code at this location".

....... TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Polaroids$iinit()

Here is my AS code


package {
    import flash.display.*;
    import flash.filters.*;
    import flash.utils.*;
    import flash.net.*;
    import flash.events.*;
    import flash.filters.DropShadowFilter;
    import caurina.transitions.*;
    public class Polaroids extends MovieClip {
        //Variables
        public var stageContainer:MovieClip;
        private var _scaleTempo:Number;
        private var _thumbStr:Number;
        private var _stageHeight:Number;
        private var _stageWidth:Number;
        private var _count:Number;
        private var _initBGHeight:Number;
        private var _initBGWidth:Number;
        //Arrays
        private var _backgroundImageArr:Array;
        private var _imageURLArr:Array;
        private var _imageCaptionArr:Array;
        private var _imagesArr:Array;
        //Bitmaps
        private var _image:Bitmap;
        private var _backgroundImage:Bitmap;
        private var _bitmap:BitmapData;
        private var _backgroundBitmap:BitmapData;
        //XML
        private var _xmlLoader:URLLoader;
        private var _imageXML:XML;
        //Holders
        private var _imageContainer:ImageContainer;
        private var _backgroundImageHolder:MovieClip;
        //Image States
        private var _activeImage = null;
        private var _previousActiveImage = null;
        //Loaders
        private var backgroundImageLoader:Loader;
        public function Polaroids() {
            //sets up initial variable values
            _count = 0;
            _backgroundImageArr=new Array;
            _imageURLArr=new Array;
            _imageCaptionArr=new Array;
            _imagesArr=new Array;
            _scaleTempo=9;
            _thumbStr = .3;
            backgroundImageLoader = new Loader();
            _stageHeight=stage.stageHeight;
            _stageWidth=stage.stageWidth;
            _backgroundImageHolder = new MovieClip();
            stageContainer = new MovieClip();
            addChild(stageContainer);
            init();
        }
        //Add Stage Listener
        private function addedToStage(e:Event):void {
            stage.addEventListener(Event.RESIZE, onResize);
        }
        /***************************************************************************
        Initialise
        ***************************************************************************/
        private function init():void {
            //Setup stage
            stage.align     = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            //Load XML
            var _xmlLoader:URLLoader=new URLLoader;
            _xmlLoader.load(new URLRequest("photos.xml"));
            _xmlLoader.addEventListener(Event.COMPLETE,processXML);
            this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
        }
        /***************************************************************************
        Process XML
        ***************************************************************************/
        private function processXML(e:Event):void {
            _imageXML=new XML(e.target.data);
            _backgroundImageArr[0] = _imageXML.@backgroundImage;
            for (var i:int=0; i < _imageXML.*.length(); i++) {
                _imageURLArr=_imageXML.image.@url;
                _imageCaptionArr=_imageXML.image.@caption;
            }
            loadImages();
            loadBackgroundImage();
        }
        /***************************************************************************
        Load Background Image
        ***************************************************************************/
        private function loadBackgroundImage():void {
            backgroundImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,addBack ground);
            backgroundImageLoader.load(new URLRequest(_backgroundImageArr[0]));
        }
        /***************************************************************************
        Add background image to stage
        ***************************************************************************/
        private function addBackground(e:Event):void {
            _backgroundImage=Bitmap(e.target.content);
            _backgroundBitmap=_image.bitmapData;
            _backgroundImage.smoothing = true;

            _backgroundImageHolder.addChild(_backgroundImage);
            _initBGHeight = backgroundImageLoader.contentLoaderInfo.height;
            _initBGWidth = backgroundImageLoader.contentLoaderInfo.width;
            if ((_initBGWidth/_initBGHeight) > (stage.stageWidth/stage.stageHeight)) {
                _backgroundImageHolder.height = stage.stageHeight;
                _backgroundImageHolder.width =  _backgroundImageHolder.height * _initBGWidth / _initBGHeight;

            } else {
                _backgroundImageHolder.width = stage.stageWidth;
                _backgroundImageHolder.height= _backgroundImageHolder.width * _initBGHeight / _initBGWidth;
            }
        }
        /***************************************************************************
        Load Images
        ***************************************************************************/
        private function loadImages():void {
            for (var i:int=0; i < _imageURLArr.length; i++) {
                var imageLoader:Loader=new Loader;
                imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,addImage);
                imageLoader.load(new URLRequest(_imageURLArr));
            }
        }

        /***************************************************************************
        Add images to MovieClip on Stage
        ***************************************************************************/
        private function addImage(e:Event):void {
            _image=Bitmap(e.target.content);
            _bitmap=_image.bitmapData;
            _image.smoothing = true;
            _imageContainer = new ImageContainer();
            _imageContainer.falseBtn.buttonMode = true;
            _imageContainer.falseBtn.doubleClickEnabled = true;
            _imageContainer.imageHolder.addChild(_image);//Add Bitmap to a MoviClip _imageContainer
            _image.x = _imageContainer.width/2 - (_image.width/2 + 15);
            _image.y = _imageContainer.height/2 - (_image.height/2 + 80) ;
            _imageContainer.imageCaption.text = _imageCaptionArr[_count];
            _imageContainer.scaleX = _thumbStr;
            _imageContainer.scaleY = _thumbStr;
            _imageContainer.rotation = 30 - 60 * Math.random();
            if (Math.round(Math.random() * 1) == 1) {
                _imageContainer.y=stage.stageHeight * Math.random() + _imageContainer.height * 2;
                if (Math.round(Math.random() * 1) == 1) {
                    _imageContainer.x=stage.stageWidth + _imageContainer.width * 2;
                } else {
                    _imageContainer.x=- _imageContainer.width * 2;
                }
            } else {
                _imageContainer.x=stage.stageWidth * Math.random() + _imageContainer.width * 2;
                if (Math.round(Math.random() * 1) == 1) {
                    _imageContainer.y=stage.stageHeight + _imageContainer.height * 2;
                } else {
                    _imageContainer.y=- _imageContainer.height * 2;
                }
            }
            //Setup Attributes
            _imageContainer.newX = Math.round((_imageContainer.width/2) + (stage.stageWidth-_imageContainer.width)*Math.random());
            _imageContainer.newY = Math.round((_imageContainer.height/2) + (stage.stageHeight-_imageContainer.height)*Math.random());
            _imageContainer.oldRotation = _imageContainer.rotation;
            _imageContainer.oldX = _imageContainer.newX;
            _imageContainer.oldY = _imageContainer.newY;
            _imageContainer.startX = _imageContainer.x;
            _imageContainer.startY = _imageContainer.y;
            _imageContainer.oldHeight = _imageContainer.scaleY;
            _imageContainer.oldWidth = _imageContainer.scaleX;
            _imageContainer.id = _count;
            _imageContainer.addEventListener(Event.ENTER_FRAME, animateImage);
            _imageContainer.addEventListener(MouseEvent.MOUSE_DOWN,dragImage);
            _imageContainer.addEventListener(MouseEvent.MOUSE_UP,dropImage);
            _imageContainer.addEventListener(MouseEvent.MOUSE_OUT, dropImage);
            _imageContainer.falseBtn.addEventListener(MouseEvent.DOUBLE_CLICK, setup_activeImage);
            _imagesArr.push(_imageContainer);//Add image reference to an Array
            _imageContainer.filters = [new DropShadowFilter(0,0,0,.9,8,8,1,1,false,false)];
            //Button Listeners
            _imageContainer.nextBtn.visible = false;
            _imageContainer.previousBtn.visible = false;
            _imageContainer.nextBtn.buttonMode = true;
            _imageContainer.previousBtn.buttonMode = true;
            _imageContainer.nextBtn.addEventListener(MouseEvent.MOUSE_DOWN, nextImage);
            _imageContainer.previousBtn.addEventListener(MouseEvent.MOUSE_DOWN, previousImage);
            //Add Container to Stage
            addChild(_imageContainer);
            stageContainer.addChild(_imageContainer);
            _count++;
        }
        /***************************************************************************
        Animate Images onto Stage
        ***************************************************************************/
        private function animateImage(e:Event):void {
            e.target.y += (e.target.newY - e.target.y) / _scaleTempo;
            e.target.x += (e.target.newX - e.target.x) / _scaleTempo;
            if (Math.round(e.target.y) == e.target.newY) {
                e.target.removeEventListener(Event.ENTER_FRAME, animateImage);
            }
        }
        /***************************************************************************
        Drag & Drop Images
        ***************************************************************************/
        private function dragImage(e:MouseEvent) {
            if (e.currentTarget != _activeImage) {
                e.currentTarget.startDrag();
                if (_activeImage == null) {
                    stageContainer.setChildIndex(DisplayObject(e.currentTarget), stageContainer.numChildren-1);
                } else {
                    stageContainer.setChildIndex(DisplayObject(e.currentTarget), stageContainer.numChildren-2);
                }
            }
        }
        private function dropImage(e:MouseEvent) {
            if (e.currentTarget != _activeImage) {
                e.currentTarget.stopDrag();
                e.currentTarget.oldX = e.currentTarget.x;
                e.currentTarget.oldY = e.currentTarget.y;
            }
        }
        /***************************************************************************
        onResize Handler
        ***************************************************************************/
        private function onResize(e:Event):void {
            for (var i:int = 0; i<_imagesArr.length; i++) {
                if (_imagesArr != _activeImage) {
                    _imagesArr.x = Math.round(stage.stageWidth * (_imagesArr.x/_stageWidth));
                    _imagesArr.y = Math.round(stage.stageHeight * (_imagesArr.y/_stageHeight));
                } else {
                    _activeImage.x = stage.stageWidth/2;
                    _activeImage.y = stage.stageHeight/2;
                }
                _imagesArr.oldX = Math.round(stage.stageWidth * (_imagesArr.oldX/_stageWidth));
                _imagesArr.oldY = Math.round(stage.stageHeight * (_imagesArr.oldY/_stageHeight));
                _imagesArr.newX = Math.round(stage.stageWidth * (_imagesArr.newX/_stageWidth));
                _imagesArr.newY = Math.round(stage.stageHeight * (_imagesArr.newY/_stageHeight));
                _imagesArr.startX = Math.round(stage.stageWidth * (_imagesArr.startX/_stageWidth));
                _imagesArr.startY = Math.round(stage.stageHeight * (_imagesArr.startY/_stageHeight));
            }
            //Background Resizer
            if ((_initBGWidth/_initBGHeight) > (stage.stageWidth/stage.stageHeight)) {
                _backgroundImageHolder.height = stage.stageHeight;
                _backgroundImageHolder.width =  _backgroundImageHolder.height * _initBGWidth / _initBGHeight;

            } else {
                _backgroundImageHolder.width = stage.stageWidth;
                _backgroundImageHolder.height= _backgroundImageHolder.width * _initBGHeight / _initBGWidth;
            }
            _stageWidth = stage.stageWidth;
            _stageHeight = stage.stageHeight;
        }
        /***************************************************************************
        Handle Selected Image
        ***************************************************************************/
        private function zoomImage():void {
            stageContainer.setChildIndex(_activeImage, stageContainer.numChildren-1);
            Tweener.addTween(_activeImage,{scaleX: 1, scaleY: 1, rotation: 0, x: _stageWidth/2 , y: _stageHeight/2, time: 1});
            _activeImage.nextBtn.visible = true;
            _activeImage.previousBtn.visible = true;
        }
        private function returnImage():void {
            stageContainer.setChildIndex(_previousActiveImage, stageContainer.numChildren-2);
            Tweener.addTween(_previousActiveImage,{scaleX: .3, scaleY: .3, rotation: _previousActiveImage.oldRotation, x: _previousActiveImage.oldX , y: _previousActiveImage.oldY, time: 1});
            _previousActiveImage.nextBtn.visible = false;
            _previousActiveImage.previousBtn.visible = false;
        }
        private function setup_activeImage(e:Event):void {
            if ((_activeImage == null) && (_previousActiveImage == null)) {
                _activeImage = e.currentTarget.parent;
                zoomImage();
            } else if (e.currentTarget.parent != _activeImage) {
                _previousActiveImage = _activeImage;
                _activeImage = e.currentTarget.parent;
                zoomImage();
                returnImage();

            } else {
                Tweener.addTween(_activeImage,{scaleX: .3, scaleY: .3, rotation: _activeImage.oldRotation, x: _activeImage.oldX , y: _activeImage.oldY, time: 1});
                _activeImage.nextBtn.visible = false;
                _activeImage.previousBtn.visible = false;
                _activeImage = null;
                _previousActiveImage = null;
            }
        }
        /***************************************************************************
        Button Handlers
        ***************************************************************************/
        private function nextImage(e:MouseEvent):void {
            var imageID = int(e.currentTarget.parent.id);
            if (imageID < _imagesArr.length - 1) {
                _previousActiveImage = e.currentTarget.parent;
                _activeImage = _imagesArr[imageID+1];
                zoomImage();
                returnImage();
            } else {
                _previousActiveImage = e.currentTarget.parent;
                _activeImage = _imagesArr[0];
                zoomImage();
                returnImage();
            }
        }
        private function previousImage(e:MouseEvent):void {
            var imageID = int(e.currentTarget.parent.id);
            if (imageID != 0) {
                _previousActiveImage = e.currentTarget.parent;
                _activeImage = _imagesArr[imageID-1];
                zoomImage();
                returnImage();
            } else {
                _previousActiveImage = e.currentTarget.parent;
                _activeImage = _imagesArr[_imagesArr.length-1];
                zoomImage();
                returnImage();
            }
        }

    }
}

This topic has been closed for replies.

2 replies

November 18, 2009

init() is called in the constructor, so most likely, the null reference is to stage. Calling init() in your addedToStage handler should resolve the problem.

HPKS77Author
Participating Frequently
November 18, 2009

Raymond,

The error is at line 55....when I debug

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Polaroids$iinit()[/Volumes/Herman's Passport/Music Rocka/RockaGallery/Polaroids.as:55]

So i'm looking in the code.....

kglad
Community Expert
Community Expert
November 18, 2009

click file/publish settings/flash and tick "permit debugging".  retest.  the line number with the error will be in the error message.

if you can't solve the problem with that information, copy and paste the line of code with the error.

HPKS77Author
Participating Frequently
November 18, 2009

ok thanks kglad,

I'll be back in 5 min