Feb 28

ColdFusion for JSP developers

Posted by James Netherton | Wednesday 28 February 2007 15:02 PM | In ColdFusion

My work colleague Kola Oyedeji has posted a great article on the merits of using ColdFusion for Java developers.

Read it on Java.net.

Feb 27

Flex stack overflow gotcha

Posted by James Netherton | Tuesday 27 February 2007 22:02 PM | In Flex

I finally got to the bottom of what was making FlashPlayer crash with a piece of code I had in one of my apps. Turns out I was getting stack overflow exceptions as a result of an event firing repeatedly.

Here’s an example application which explains what I was doing:

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

	<mx:Script>
		<![CDATA[
			import mx.events.DataGridEvent;
			import mx.collections.ArrayCollection;

			[Bindable]
			private var gridData:ArrayCollection = new ArrayCollection();

			private function setData():void
			{
				for(var i:Number = 1; i < 10; i++)
				{
					var obj:Object = new Object();
					obj.id = i;
					obj.name = "Name " + i;

					gridData.addItem(obj);
				}
			}

			private function validateEdit(event:DataGridEvent):void
			{
                var newVal:String = event.currentTarget.itemEditorInstance.text;				

				if(newVal.length > 150 && event.reason == "newRow")
				{
					mx.controls.Alert.show("Value cannot exceed 150 characters in length");
					return;
				}
			}

		]]>
	</mx:Script>

	<mx:DataGrid width="100%"
				 height="100%"
				 dataProvider="{gridData}"
				 itemEditEnd="validateEdit(event)"
				 editable="true">
		<mx:columns>
			<mx:DataGridColumn dataField="id" headerText="Person Id"/>
			<mx:DataGridColumn dataField="name" headerText="Person Name" editable="true"/>
		</mx:columns>
	</mx:DataGrid>	

</mx:Application>

It’s quite straightforward. I populate a DataGrid component with some dummy data. The person name column is made editable. Whenever a value is entered into the DataGrid cell the validateEdit method traps the itemEditEnd event and checks to see whether the string entered was greater than 150 characters.

If the condition evaluates to true, an Alert box is displayed.

Run the application, click one of the editable DataGrid cells and enter a string longer than 150 characters, the result should be that your browser or Flash Player will crash.

Now add a trace statement into the validateEdit method:

trace(event.reason)

Run the application again and enter another string longer than 150 characters into the editable DataGrid cell.

The application will crash again, but the trace output should reveal that the itemEditEnd event was fired over a hundred times which means that the Alert popup will be attempted to be displayed each time – obviously not very performant and hence the stack overflow!

The trace also reveals that all of the event reasons were for type ‘other’, but the actual item edit event was for type ‘newRow’.

So the solution is quite simple, we simply add an extra condition into the if statement that checks the character length.

if(newVal.length > 150 && event.reason == "newRow")

Rerun the application and hopefully no more stack overflow errors will occur :) .