A Flash Developer Resource Site

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    3

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

    I can't solve this, i need help.

    I am trying to load another b.swf into this a.swf
    if i try to load a swf without class it works fine.
    a.swf have no class, but b.swf have.
    I encounter this error.

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

    What is the solution?
    a.fla
    Actionscript Code:
    stop();

    function startLoad()
    {
    var imageRequest:URLRequest = new URLRequest("b.swf");
    var imageLoader:Loader = new Loader();
    imageLoader.load(imageRequest);
    addChild(imageLoader);
    }

    function onCompleteHandler(loadEvent:Event)
    {
            addChild(loadEvent.currentTarget.content);
    }
    function onProgressHandler(mProgress:ProgressEvent)
    {
    var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
    trace(percent);
    }
    startLoad();
    Mygame.as
    Actionscript Code:
    package {
        import flash.display.MovieClip
        import flash.display.Sprite
        import flash.display.Bitmap
        import flash.display.BitmapData
        import flash.display.Graphics
        import flash.events.Event
        import flash.events.MouseEvent
        import flash.utils.ByteArray
        import flash.utils.setInterval
        import flash.utils.setTimeout
        import flash.geom.Point
        import flash.text.TextField
        import flash.display.BlendMode
        import flash.display.StageScaleMode
        import flash.display.StageAlign
        import flash.text.TextFormat
       
        import playerio.*
        import sample.ui.Prompt
        import sample.ui.Chat
        import sample.ui.Lobby
       
        public class MyGame extends MovieClip{
            private var connection:Connection
            private var lobby:Lobby
            private var questions:Array=new Array();
            private var answers:Array=new Array();

            [Embed(source="c.xml", mimeType="application/octet-stream")]
            private static const MyData:Class;
           
            function MyGame(){
                stop();
               
               
                PlayerIO.connect(
                    stage,                                //Referance to stage
                    "quiz-sfhwfbj70ectvbcjazv2tw",    //Game id (Get your own at playerio.com)
                    "public",                            //Connection id, default is public
                    "name",                        //Username
                    "",                                    //User auth. Can be left blank if authentication is disabled on connection
                    handleConnect,                        //Function executed on successful connect
                    handleError                            //Function executed if we recive an error
                );  
                stage.scaleMode = StageScaleMode.NO_SCALE
                stage.align = StageAlign.BOTTOM
            }
           
            private function handleConnect(client:Client):void{
                trace("Sucessfully connected to player.io");
               
                //Set developmentsever (Comment out to connect to your server online)
                client.multiplayer.developmentServer = "localhost:8184";
               
                //Create pr join the room test
                client.multiplayer.createJoinRoom(
                    "",                            //Room id. If set to null a random roomid is used
                    "bounce",                            //The game type started on the server
                    false,                                //Should the room be visible in the lobby?
                    {},                                    //Room data. This data is returned to lobby list. Variabels can be modifed on the server
                    {},
                    handleJoin,                            //Function executed on successful joining of the room
                    handleError                            //Function executed if we got a join error
                );
                //Create lobby
                lobby = new Lobby(client, "mygame", handleJoin, handleError)

                //Show lobby (parsing true hides the cancel button)
                lobby.show(true);
            }
           
           
            private function handleJoin(connection:Connection):void{
                trace("Sucessfully connected to the multiplayer server");
                           
                var byteArray:ByteArray = new MyData() as ByteArray;
                var xml:XML = new XML(byteArray.readUTFBytes(byteArray.length));
                var loop = 5;
               
                for (var i=0;i<loop;i++)
                {
                    questions[i]=xml.ques[i].q1;
                    //questions[i]=loop;
                    answers[i]=[xml.ques[i].op1,xml.ques[i].op2,xml.ques[i].op3];
                    //answers[i]=loop;
                }
                gotoAndStop(2);
                stop();

                /*
                trace(questions[1]);
                trace(answers[1]);
                trace(answers[1][0]);
                trace(answers[1][1]);
                trace(answers[1][2]);
                */

                            //Add chat to game
                var chat:Chat = new Chat(stage, connection);
               
                //Add listener for messages of the type "hello"
                connection.addMessageHandler("hello", function(m:Message){
                    trace("Recived a message with the type hello from the server");            
                })
               
                //Add message listener for users joining the room
                connection.addMessageHandler("UserJoined", function(m:Message, userid:uint){
                    trace("Player with the userid", userid, "just joined the room");
                })
               
                //Add message listener for users leaving the room
                connection.addMessageHandler("UserLeft", function(m:Message, userid:uint){
                    trace("Player with the userid", userid, "just left the room");
                })
               
                //Listen to all messages using a private function
                connection.addMessageHandler("*", handleMessages)
               
            }
           
            private function handleMessages(m:Message){
                trace("Recived the message", m)
            }
           
            private function handleDisconnect():void{
                trace("Disconnected from server")
            }
           
            private function handleError(error:PlayerIOError):void{
                trace("got",error)
                gotoAndStop(4);

            }
        }    
    }

  2. #2
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    You have this function where you call "load()" method
    Actionscript Code:
    stop();

    function startLoad()
    {
    var imageRequest:URLRequest = new URLRequest("b.swf");
    var imageLoader:Loader = new Loader();
    imageLoader.load(imageRequest);
    addChild(imageLoader);
    }

    function onCompleteHandler(loadEvent:Event)
    {
            addChild(loadEvent.currentTarget.content);
    }
    function onProgressHandler(mProgress:ProgressEvent)
    {
    var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
    trace(percent);
    }
    startLoad();

    But where's the event listener for the loader.contentLoaderInfo object?

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The error is caused by the use of stage in the constructor for MyGame. The stage property is null until the instance is added to the display list, and it can't be added to the display list until after it's created. Flash cheats a bit when you use a class as a document class, so it works there, but not when loading into a loader.

    Use an event listener for ADDED_TO_STAGE and do all your stage dependent initialization in there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center