views:

242

answers:

4

Hello,

I am trying to load an image in Flex (AS3) which already exists:

<mx:Image id="Img"/>

With this code:

Img.load('http://www.path.com/image.jpg');

The path is verified to work, but I always get this error: TypeError: Error #1009: Cannot access a property or method of a null object reference.

I used the code from the documentation and still receive this error!

+1  A: 

when using an mx.Image tag, try using IMG.source = "http://path.com/image.jpg"; IMG.load(); otherwise, you might just have to set it up as an URLRequest; null object references are quite common however, and could come from a number of different vars that you are trying to access before they are ready. make sure that you aren't trying to access IMG before the flex app is ready. in your code, on the first line of MXML where you define your document, make sure that there is a creationComplete="init()" // or whatever your initial function is, and then assign the IMG source within that function. this ensures that as vars are not attempting to access anything defined in the mxml that isn't ready yet/

Ian
No it is not before the app is ready, this piece of code is pretty deep in the code, and to test it is in the init function. Setting the source gives me the exact same error, even without calling the load() function.
Rogier21
are you getting this error on compile?, or when you try to load?only other things i can think of are using double quotes: " " instead of single quotes(assuming that the above was taken directly from the source), or making sure your crossdomain.xml is set correctly.
Ian
A: 

When are you calling the function? And while it's not standard practice to call the load method on an Image control (setting its source property is more common), doing so should be fine:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="onInitialize()">

    <mx:Script>
        <![CDATA[

            private function onInitialize():void
            {
                Img.load("http://turbonerd.com/media/images/roaming/o/20100203192528.jpg");
            }

        ]]>
    </mx:Script>

    <mx:Image id="Img" />

</mx:Application>

It does matter when you call that method, though; if you're getting a null reference on that specific line, then the Image control is definitely not there.

Make sure you wait at least until the container's initialize event fires (as above) before attempting to access the control in code. If you're adding the control dynamically at runtime, then you should wait until the control's initialize event, to be sure there's an object there to work with.

Christian Nunciato
Ok the problem was that the image component was placed on a canvas which was on a stackview, and apparently you cannot access these elements while this canvas is invisible!
Rogier21
A: 

Ok the problem was that the image component was placed on a canvas which was on a stackview, and apparently you cannot access these elements while this canvas is invisible

Rogier21
A: 

With your answer of the image being on a hidden child of a viewstack, that changes the nature of your problem from other peoples point of view. You can actually access items on the children of that viewstack without making them visible by changing the creationPolicy of the viewstack to "all".

invertedSpear
Nice, thanks didn't know that.
Rogier21