Error #1010 on a Dynamic XML Photo Gallery

Hey all, first post so forgive me.

I posted on another forum and haven’t gotten any help yet so any help is greatly appreciated!

I’m building a dynamic XML photo gallery/case study application that allows users to filter relevant results and then view the corresponding text/photos. So far i’ve used list components to scan my XML document and return the results for the filters, and then add thumbnails to a panel. When you click on the thumbnail, it brings up the first of a series of images, which i load into a Loader instance and add it to the stage using addChild();

The image sequence is then controlled by a series of navigational buttons, of which currently i only have “Next” and “Previous”. The buttons work by incrementing/deincrementing a variable that is the index of the image currently being displayed, so when you hit next, it adds/subtracts 1 from that variable, and then uses that number for the index of my <image> tag in the XML doc. All works fine until you reach the end of the images. I have a conditional that tests if the currentImg variable is equal to the length(); property of the XML tag, and if it is, it resets the currentImg value to 0. And vice versa for the previous button. But when the file is published, and i reach the last image and hit the next button, i get this error:

TypeError: Error #1010: A term is undefined and has no properties.
at MethodInfo-346()

The strange thing is, that if i hit next again, it does what it is supposed to do by going to the first image. Then if i cycle through the images once again the same thing happens. And the same scenario and error occurs when i hit the previous button on the first image. Can anyone tell me why this is? if i need to somehow remove the images from memory, or if they are still in memory and i need to access them a different way the second time around? this is very frustrating. Thanks in advance!