4 Replies Latest reply on Jul 20, 2010 4:47 PM by Flex harUI

    Simple SharedObject question (I think)

    jeffcg2 Level 1

      I am trying to understand how to write and read from the SharedObject to remember a login.

       

      This was simple in Flex Builder 3, used the wizard and it built the login plumbing whether you wanted it or not.

       

      I tried to get something from the video below, but obviouly there is more.

       

      I run the code below and get:

       

      TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at logtest/saveData()[C:\Inetpub\wwwroot\tiptest\src\logtest.mxml:26]
      at logtest/___logtest_Button1_click()[C:\Inetpub\wwwroot\tiptest\src\logtest.mxml:48]

       

      The sharedobject is null.

       

      What am I not understanding.

       

      http://tv.adobe.com/watch/adc-presents/using-shared-objects-in-flex-applications

       

       

      <?xml version="1.0" encoding="utf-8"?>

      <s:Application

       

       

      xmlns:fx="http://ns.adobe.com/mxml/2009"

      xmlns:s="

      library://ns.adobe.com/flex/spark"

      xmlns:mx="

      library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

       

       

      <s:layout>

       

       

      <s:BasicLayout/>

       

       

      </s:layout>

       

       

      <fx:Script>

      <![CDATA[

       

       

      import flash.net.SharedObject;

       

       

      private var sharedObj:SharedObject;

       

       

      private function initSharedObject():void

      {

      SharedObject.getLocal(

       

      "myContacts" );

       

       

      if ( sharedObj.size > 0 )

      {

      txtArea.text = sharedObj.data.contacts;

      }

      }

       

       

      private function saveData(event:MouseEvent):void

      {

      sharedObj.data.contacts = txtInput.text;

      sharedObj.flush();

      lbl.text =

       

      "Data Stored Locally";

      }

       

       

      private function deleteData(event:MouseEvent):void

      {

      sharedObj.clear();

      lbl.text =

       

      "Local Data Deleted";

      }

      ]]>

       

      </fx:Script>

       

       

      <fx:Declarations>

       

       

      <!-- Place non-visual elements (e.g., services, value objects) here -->

       

       

      </fx:Declarations>

       

       

      <s:TextArea x="268" y="120" id="txtArea"/>

       

       

      <s:Label id="lbl" x="268" y="279" width="161">

       

       

       

       

      </s:Label>

       

       

      <s:Button x="160" y="341" label="Save" click="saveData(event)"/>

       

       

      <s:Button x="249" y="341" label="Delete" click="deleteData(event)"/>

       

       

      <s:Button x="344" y="341" label="Button"/>

       

       

      <s:TextInput x="268" y="68" id="txtInput"/>

      </s:Application>

        • 1. Re: Simple SharedObject question (I think)
          Flex harUI Adobe Employee

          I don't think I saw a call to initSharedObject or storing of the

          SharedObject in the sharedObject property

          • 2. Re: Simple SharedObject question (I think)
            rtalton Level 4

            It looks like your LSO is null because you have not populated it yet with the initSharedObject() method.

            Try calling initSharedObject() from the creationComplete event of the application.

            • 3. Re: Simple SharedObject question (I think)
              jeffcg2 Level 1

              I added the creation complete and get this error now. I don't know what you mean by "storing of the Shared Object in the sharedObject property"

               

               

              <s:Application

               

               

              xmlns:fx="http://ns.adobe.com/mxml/2009"

              xmlns:s="

              library://ns.adobe.com/flex/spark"

              xmlns:mx="

              library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"

              creationComplete="initSharedObject()"

              >

               

              TypeError: Error #1009: Cannot access a property or method of a null object reference.
              at logtest/initSharedObject()[C:\Inetpub\wwwroot\tiptest\src\logtest.mxml:19]
              at logtest/___logtest_Application1_creationComplete()[C:\Inetpub\wwwroot\tiptest\src\logtest .mxml:5]
              at flash.events::EventDispatcher/dispatchEventFunction()
              at flash.events::EventDispatcher/dispatchEvent()
              at mx.core::UIComponent/dispatchEvent()[E:\dev\4.x\frameworks\projects\framework\src\mx\core \UIComponent.as:12528]
              at mx.core::UIComponent/set initialized()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:1627]
              at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.x\frameworks\projects\framewo rk\src\mx\managers\LayoutManager.as:759]
              at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.x\frameworks\projects \framework\src\mx\managers\LayoutManager.as:1072]

              • 4. Re: Simple SharedObject question (I think)
                Flex harUI Adobe Employee

                sharedObj = SharedObject.getLocal( "myContacts" );