4 Replies Latest reply on Mar 10, 2011 4:11 AM by TheGarfunkal

    Changing Image Source programmatically

    TheGarfunkal Level 1

      Hi

       

      I've got a problem changing my image source based on a value in a variable.

       

      The following function gets kicked off, but I get a TypeError: Error #1009: Cannot access a property or method of a null object reference. error.

       

      I've tried embedding, and the solution you see below (which i found here - as I am chaning it at run time http://forums.adobe.com/message/116979#116979), but no luck

       

      I am certain its how I'm mapping the png, but I can't be sure..... any suggestions?  I've been banging my head against my desk for ages on this!

       

      function (note - the trace proves that the value to change the image is correct)

       

       

      public function LeversComplete():void{

       

          if(PodContentBase._ProjectDetail.getItemAt(0).IndustryLeadingMastery == "N")

                trace("Value" + PodContentBase._ProjectDetail.getItemAt(0).IndustryLeadingMastery)

                  imgIndustry.source = "/assets/bigcheese.png";

           else {imgIndustry.source = "assets/Nocheese.png"} 

      }

       

       

      Image (note - this is set as a defualt - and defaults in fine)

       

       

             <mx:Image id="imgIndustryleadingtechnicalmastery"

                                    source="assets/bigcheese.png"/>

       

       

      Picture of Directory Structure:

       

      path.JPG

        • 1. Re: Changing Image Source programmatically
          UbuntuPenguin Level 4

          A few questions.. The "id" parameters are different in your example.  In your actionscript, the id is "imgIndustry" but in the mxml the id is "imgIndustryleadingtechnicalmastery".  Moving on, how do you know your image has been created yet, that is often a source of null pointer exceptions.  Whenever I think that is a problem, I do something like this

           

          ...AS3..

          [Bindable] public var imageSource:Object;

           

           

          ...MXML...

          <Image source="{imageSource}" />

           

          If in fact the problem is that the image isn't created, this will solve it because the image will go and get its source when it is good and ready.

          • 2. Re: Changing Image Source programmatically
            Nuwan,Chamara Level 1

            Hi,

             

            First in the LeversComplete() method, the id of the image you are changing the source property is "imgIndustry", but the id of the image in the <image> tag is "imgIndustryleadingtechnicalmastery". May be it's not working because you are changing the source property of some other image.

             

            And I think you don't need a "/" in front of assets/bigcheese.png in the LeversComplete() method.

             

            If these are not working you could have a debug session and see which property is null and gives the error.

            • 3. Re: Changing Image Source programmatically
              TheGarfunkal Level 1

              Hi. Yeah, the ids are correct in my code. I changed them for

              readability on here, just really badly. I've tried a \ before asset

              to.

               

              I'll try the penguin method of checking the image is created. Thinking

              back I am sure that is it as this is called when the query run.  I'll

              report back. Cheers.

              • 4. Re: Changing Image Source programmatically
                TheGarfunkal Level 1

                Hi - all done - dead right - The image didnt exisit and i'm trying to stuff things in it - what a divvy -

                Anyways, this worked -    I check if the image exists before I try and change it.            

                 

                 

                public function LeversComplete():void{

                 

                          if (imgIndustryleadingtechnicalmastery){

                 

                               if(PodContentBase._ProjectDetail.getItemAt(0).IndustryLeadingMastery == "N") {imgIndustryleadingtechnicalmastery.source="assets/nocheese.png"}

                               else {imgIndustryleadingtechnicalmastery.source = "assets/bigcheese.png"} 

                                }

                }