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

StackOverflow https://stackoverflow.com/questions/22477917

Question

In my Form, I have a TextInput and a FileReference with Browse Button. In this form, when I submit without selecting any image file it will throw an error like this "TypeError: Error #1009: Cannot access a property or method of a null object reference."

My need: If I submit the form without selecting any image file, it has to be take a dummy image programmatically. I don't have any idea about it.

My sample code:

        <s:Form id="mainForm" height="100%" width="100%" left="10%" right="10%" top="10%">
            <s:FormItem id="nameLabel" label="Employee Name">
                <s:TextInput id="employeeName" rollOver="validateAndSubmit()"/>
            </s:FormItem> 
            <s:FormItem id="imageLabel" label="Image">
                <mx:HBox>
                    <s:TextInput id="employeeImageName" editable="false" showErrorSkin="true" showErrorTip="false"/>
                    <s:Button id="imageButton" label="Browse" click="onBrowseButtonClicked(event)"/>
                </mx:HBox >
            </s:FormItem>
            <s:FormItem>
                <s:layout>
                    <s:HorizontalLayout gap="10"/>
                </s:layout>
                <s:Button id="submitButton" label="Submit" click="storeInputs(event)"/>
                <s:Button id="clearButton" label="clear" click="clearInputs()"/>
            </s:FormItem>
        </s:Form>
        <s:DataGrid width="100%" height="100%" dataProvider="{arrayCollection}">
        <s:columns>
            <s:ArrayList>
                <s:GridColumn headerText="Name" dataField="name" />
                <s:GridColumn headerText="Employee" id="imageColumn" dataField="imageData"/>
                <s:itemRenderer>
                    <fx:Component>
                        <s:GridItemRenderer>                                    
                            <s:Image id="image"  source="{data.imageData}"  visible="true" height="80" width="100"/>
                        </s:GridItemRenderer>
                    </fx:Component>
                </s:itemRenderer>
            </s:ArrayList>
        </s:columns>
    </s:DataGrid>

The storeInput()

        private function storeInputs(event:MouseEvent) : void
        {
            arrayCollection.addItem({
                name : employeeName.text, 
                imageData: loadFile.data
            });
        }

Edit: My onBrowseButtonClicked() code:

        public var loadFile:FileReference; 
        [Bindable]private var arrayCollection : ArrayCollection = new ArrayCollection ();

        private function onBrowseButtonClicked(event : MouseEvent) : void
        {
            loadFile = new FileReference();

            loadFile.addEventListener(Event.SELECT, selectHandler);
            var fileFilter:FileFilter = new FileFilter("Images: (*.jpeg, *.jpg, *.gif, *.png)", "*.jpeg; *.jpg; *.gif; *.png");
            loadFile.browse([fileFilter]);
        }

        private function selectHandler(event:Event):void
        {
            loadFile.load();    
        }

If I doesn't select any file using the Browse Button, it has to be load a dummy image automatically. How can I do it?

Was it helpful?

Solution

First make it your Image tag as itemRenderer like this

<s:GridColumn headerText="Employee" id="imageColumn" dataField="imageData" itemRenderer="components.GridItemRender"/>

And the GridItemRender.MXML

    <fx:Script>
    <![CDATA[           
        import mx.events.FlexEvent;         

        protected function emp_renderHandler(event:Event):void
        {
            if (data.imageData == null)
            {
                image.source = "http://localhost/demo/images/male.jpg";
            }
        }

    ]]>
</fx:Script>

<s:Image id="image"  source="{data.imageData}"  width="80" height="100" render="emp_renderHandler(event)"/>

I think this code may help you...

OTHER TIPS

you need something like that: you may need to change error event type, i was used e:IOErrorEvent for my URLLoader object, but i'm not sure it works for your situation

private function selectHandler(event:Event):void
{
   try
   {

      loadFile.load();

   } catch (e:Error)
      {
         loadFile = dummypicture;
         loadFile.load();
      }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top