Welcome, Guest
  • Author Topic: Flex 3: How to resolve error with DateField being Null value  (Read 16231 times)

    jarmanje

    • Senior Programmer
    • ****
    • Posts: 334
      • View Profile
    Hello,

    I have been playing with Flex on and off for a while now. I'm getting the hand of it more, but it's simple problems I can't resolve!

    I have a DateField which is loaded from a mysql Database, it all works fine. My problem is when creating a new record in my database (so my array is set to nothing at this point..) and when the value in the database is NULL

    here is my datagrid:
    Code: [Select]
    <mx:DateField id = "orderdate" formatString="YYYY-MM-DD" width="170" showToday="true" text ="{currentOrder.date}"/>
    and here is my error on line 446 of DateField.as:
    Code: [Select]
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at mx.controls::DateField$/stringToDate()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\controls\DateField.as:446]
    at mx.controls::DateField/displayDropdown()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\controls\DateField.as:2118]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.core::UIComponent/callLaterDispatcher2()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8565]
    at mx.core::UIComponent/callLaterDispatcher()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8508]

    Perhaps I add a check if the array value is null and if so make it todays date? How can i do this?



    Also, just very quickly. How can i change the .x positon of an addchild. It is top left at the moment...
    Code: [Select]
    <mx:AddChild relativeTo="{this}"
    position="lastchild"
    creationPolicy="all">
    <forms:OrderForm id="OrderForm"
    cancel="currentState=null"
    save="saveRecord(event)"/>
    </mx:AddChild>

    « Last Edit: 09/25/08, 18:49 by jarmanje »

    Ronald Wernecke

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 6203
      • View Profile
      • Professional Support
      • Email
    I would set the date at server side with the creating query (set dateField=now() )

    Every components parameters can be set by initializing (creating) the instance of the object - just give it an asociative array of settings (like {x:10,y:15,text:'initial Value'}
    happy flashing
    8)
    Ronald

    jarmanje

    • Senior Programmer
    • ****
    • Posts: 334
      • View Profile
    would it be possible to do this:

    <mx:DateField id = "orderdate" formatString="YYYY-MM-DD" width="170" showToday="true" If(currentOrder.date==NULL) {  text ="getDate()") } else { text ="{currentOrder.date}" } ="getDate()"/>

    but the MX code :D

    jarmanje

    • Senior Programmer
    • ****
    • Posts: 334
      • View Profile
    I've tried this:

    In as
    Code: [Select]
    [Bindable]
    public var currentOrder:Object = new Object();

    private function getDate():String
    {
        if( currentOrder==null )
            return (new Date()).toString;
        return currentOrder.date;
    }

    and in mx

    Code: [Select]
    <mx:DateField id = "orderdate" formatString="YYYY-MM-DD" width="170" showToday="true" text ="getDate()"/>

    I get an error code though:

    Severity and Description   Path   Resource   Location   Creation Time   Id
    1067: Implicit coercion of a value of type Function to an unrelated type String


    Jorge Solis

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 14616
      • View Profile
    You should use some AS handler to populate the Datefield, automatic binding will not help, except if you fix this on server side (PHP) by replacing NULL values

    Jorge

    jarmanje

    • Senior Programmer
    • ****
    • Posts: 334
      • View Profile
    Hi Jorge, Thanks for your reply

    I have fixed this server side. But now my code still has issues when creating a new record. The problem here is not server side, because i'm just using an empty array.

    I have one main.mxml with a datagrid, when you click on the button 'new'
    it then addschild my file OrderForm.mxml

    inside main.mxml
    Code: [Select]
    <mx:Button label="New"
    enabled="{currentState != 'detail'}"
    click="newRecord()"/>

    and inside <![CDATA[ of main.mxml
    Code: [Select]
    private function newRecord():void
    {
    OrderForm.currentOrder = {orderid:0};
    currentState = "detail";
    }

    then inside my OrderForm.mxml is
    Code: [Select]
    <mx:DateField id = "orderdate" formatString="YYYY-MM-DD" width="170" showToday="true" text ="{currentOrder.date}"/>

    The currentOder is an array like this
    Code: [Select]
    public var currentOrder:Object = new Object();


    I think the problem is a lot more simple than I am explaining it. It just want to create a blank entry form, with todays date in that field auto filled in. But if i am editing a record, it will fill in the data respectively (as it does perfectly now)

    Jorge Solis

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 14616
      • View Profile
    Here's a simple default:

    Code: [Select]
    <mx:FormItem label="Date (live)">
    <mx:DateField id="edit_date_txt" showToday="true" formatString="DD/MM/YYYY" selectableRange="{{rangeStart : new Date()}}"
    creationComplete= "try{selectedDate="{new Date(mainList.selectedItem.Date)}} catch(e:Error){}"/>
    </mx:FormItem>

    In my case, Date is a property of a List item, the try/catch block avoid to get the error, the showToday force to show current Date if nothing is given

    Jorge

    jarmanje

    • Senior Programmer
    • ****
    • Posts: 334
      • View Profile
    thanks a lot jorge.

    Will test this in the morning. Looks exactly what i needed though, just catch the null value and turn it into todays date..