msgbartop
Just a little about development! Think before you write!
msgbarbottom

06 Feb 09 Tips And Tricks: Adobe Flex – avoid accessing null objects

One of the many possible ways of iterating over an array in Flex is:

1
2
3
4
5
private function iterateOverArray(arr : Array) : void {
  for(var i : int = 0; i < arr.length; i++){
    doSomethingWith(arr[i]);
  }
}

But this code will cause problems if the Array is null, to avoid this kind of problems you can use the following code instead:

1
2
3
4
5
private function iterateOverArray(arr : Array) : void {
  for each(var o : Object in arr){
    doSomethingWith(o);
  }
}

This way we do not need two variable declarations, and we do not try to read the length of a possible null variable.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Tags: , ,