Skip to content
May 6 / Sidney de Koning

Tracing XMLList within a switch statement returns null

Just found really nasty bug in Flash; When using XML Filtering within a switch statement it will fail when assigning that value to an XMLList variable and trace null. (Even though there is data)

When you declare the variable outside/above of the switch it does work. What can also be done is assigning the variable to a function that does the filtering.

Apparently you cant assign and set a variable on the same line within a switch because of a scoping issue. Also checked JIRA, Adobe’s BUG database, and it is already filed :)

Here are some examples:

//FAILS!
switch(someName) {
	case "myName":
		trace(_xml.myItems.(@id == "1")) // traces the inside of XMLList
		var items:XMLList = _xml.myItems.(@id == "1");
		trace( items ); // traces null
	break;
}
// This will give us a TypeError: Error #1010: A term is undefined and has no properties. 

//SUCCESS!
var items:XMLList;
switch(someName) {
	case "myName":
		trace(_xml.myItems.(@id == "1")) // traces the XMLList
		items = _xml.myItems.(@id == "1");
		trace( items ); // traces the inside of XMLList
	break;
}

//ALSO SUCCESS!
var items:XMLList;

switch(someName) {
	case "myName":
		trace(_xml.myItems.(@id == "1")) // traces the XMLList
		items = getFilteredXML(_xml.myItems, "id", "1");
		trace( items ); // traces the inside of XMLList
	break;
}

function getFilteredXML(pList:XMLList, pAttribute:String, pValue:String):XMLList {
	return pList.(attribute(pAttribute) == pValue);
}

Hope this saves somebody else a couple of hours bughunting … :)

Please consider to buying me a coffee.

7 Comments

Leave a comment
  1. gerb / Jul 3 2009

    It did. Thank you very much!

  2. Rafal / Sep 17 2009

    Where is You temple?
    I will pray to You starting from Yesterday!

  3. Sidney de Koning / Sep 17 2009

    Its in the North ;)

  4. Lynda / Sep 7 2010

    I was going insane over this problem! I’d already lost a few hours, but this saved me from wasting a few more. Cheers :)

  5. interim / Jan 11 2011

    I really enjoyed reading this post, big fan. Keep up the good work and
    please tell me when can you publish more articles or where can I read
    more on the subject?

  6. hsv / Apr 21 2011

    Thank’s for this post! It just saved me a lot of time!

  7. Christian Louboutin / Jul 18 2011

    My organization is very enjoyed in this side. Its a sexy topic. It aid me very much to eliminate some problems. Its opportunity are incredibly fantastic and dealing style so speedy. It could very well be help all of you. Thanks.

Leave a comment