Skip to main content
October 7, 2010
Question

Shared Object issue in chat program

  • October 7, 2010
  • 1 reply
  • 580 views

Hi Folks...

I have this set of codes for a simple chat program using SharedObjects. When I run the program, I type my name into the 'chatName' box and my message in the 'textInput'box. However when click on the 'Send' button or press the Enter key, I am getting the following error:

" TypeError: Error #1009: Cannot access a property or method of a null object reference.
at TextChat/sendMsg() "

As mentioned in the error message I looked at the 'sendMsg()' function. I realised the problem was comming from

the line :

text_so.setProperty ("msg",chatName.text +": "+ textInput.text);

I am wondering how 'text_so' can be a null object reference. It was already instantantiated and setup earlier in

the code. I hope someone can shed light on this. The relevant lines of code are below. Thanks guys.

//Setting up of the shared object function

private function doSO (e:NetStatusEvent):void
{
   good=e.info.code == "NetConnection.Connect.Success";
   if (good)
   {
    //Set up shared object
    text_so=SharedObject.getRemote("test",nc.uri,false);
    text_so.connect (nc);
    text_so.addEventListener (SyncEvent.SYNC,checkSO);
   }
}

// The 'sendMsg' function

private function sendMsg (e:MouseEvent):void
{
   noName=(chatName.text=="<Enter Name>" || chatName.text=="");
   if (noName)
   {
    textArea.appendText ("You must enter your name \n");
   }
   else
   {
    text_so.setProperty ("msg",chatName.text +": "+ textInput.text);
    textArea.appendText (chatName.text +": "+textInput.text + "\n");
    textInput.text="";
   }
}

    This topic has been closed for replies.

    1 reply

    October 7, 2010

    Are you defining text_so as a class property, or just defining it in the doSO function. If the later, that's the problem. If you define a variable within a function, the scope of the variable is the function, so it won't be available after the function is done executing.

    October 8, 2010

    Hi JayCharles...

    Thanks for the message, text_so was defined as a class property and yet I have this problem. I will post the full source code here.

    package
    {
    import fl.controls.TextArea;
    import fl.controls.Button;
    import fl.controls.TextInput;
    import flash.display.Sprite;
    import flash.events.SyncEvent;
    import flash.events.NetStatusEvent;
    import flash.events.MouseEvent;
    import flash.events.FocusEvent;
    import flash.net.SharedObject;
    import flash.net.NetConnection;
    import fl.events.ComponentEvent;

    public class TextChat extends Sprite
    {
      private var button:Button;
      private var text_so:SharedObject;
      private var nc:NetConnection;
      private var textArea:TextArea;
      private var textInput:TextInput;
      private var chatName:TextInput;
      private var rtmpGo:String;
      private var good:Boolean;
      private var catchKey:Boolean;
      private var noName:Boolean;
     
     
      public function TextChat ()
      {
       //Set up UIs
       textArea=new TextArea();
       textArea.setSize (500,280);
       textArea.move (20,54);
       addChild (textArea);
       textInput=new TextInput();
       textInput.setSize (500,24);
       textInput.move (20,340);
       textInput.addEventListener (ComponentEvent.ENTER,checkKey);
       addChild (textInput);
      
       button=new Button();
       button.width=50;
       button.label="Send";
       button.move (20,370);
       button.addEventListener (MouseEvent.CLICK,sendMsg);
       addChild (button);
      
       chatName=new TextInput;
       chatName.setSize (100,24);
       chatName.move (80, 370);
       chatName.text="<Enter Name>";
       chatName.addEventListener (FocusEvent.FOCUS_IN,cleanName);
       addChild (chatName);
      
       rtmpGo = "rtmp://192.168.0.11/basicSO";
       nc = new NetConnection( );
       nc.connect (rtmpGo);
       nc.addEventListener (NetStatusEvent.NET_STATUS,doSO);
      }
     
    private function doSO (e:NetStatusEvent):void
    {
       good=e.info.code == "NetConnection.Connect.Success";
       if (good)
       {
        //Set up shared object
        text_so=SharedObject.getRemote("test",nc.uri,false);
        text_so.connect (nc);
        text_so.addEventListener (SyncEvent.SYNC,checkSO);
       }
    }



    private function checkSO (e:SyncEvent):void
    {
       for (var chng:uint; chng<e.changeList.length; chng++)
       {
        switch (e.changeList[chng].code)
        {
         case "clear" :
         break;
        
         case "success" :
         break;
        
         case "change" :
         textArea.appendText (text_so.data.msg + "\n");
         break;
        }
       }
    }

    private function cleanName (e:FocusEvent):void
    {
       chatName.text="";
    }

    private function sendMsg (e:MouseEvent):void
    {
       noName=(chatName.text=="<Enter Name>" || chatName.text=="");
       if (noName)
       {
        textArea.appendText ("You must enter your name \n");
       }
       else
       {
       
        text_so.setProperty ("msg",chatName.text +": "+ textInput.text);
        textArea.appendText (chatName.text +": "+textInput.text + "\n");
        textInput.text="";
       }
    }

    private function checkKey (e:ComponentEvent):void
       {
        noName=(chatName.text=="<Enter Name>" || chatName.text=="");
        if (noName)
        {
         textArea.appendText ("You must enter your name \n");
        }
       
        else
        {
         text_so.setProperty ("msg",chatName.text +": "+ textInput.text);
         textArea.appendText (chatName.text +": "+textInput.text + "\n");
         textInput.text="";
        }
       }
    }
    }

    Thanks and regards

    October 8, 2010

    Hi..

    I think I have solved the problem for now.  I have changed my rtmp setting from :

    rtmpGo = "rtmp://192.168.0.11/basicSO";

    to

    rtmpGo = "rtmp:/basicSO";

    This solves the problem. The earlier declareation with the ip address was from a sample code, however when I run locally I need to change it to point to the 'application' directory within my fms3.5 directory. I will eventually be running the program from 2 machines, where the connection has to be done via a network and not locally, the problem might crop up again. I will see how it goes. Thanks..