Add / remove from XMLList in a loop

I am trying to parse some XML that I received via e4x in an HTTPService. The loop works, and for each episode on the list, it goes through a loop. However, I am getting the following error when it tries to add to the XMLList.

TypeError: Error #1009: Cannot access a property or method of a null object reference.

      

I am trying to query my local SQLite database and see if the episode exists (works) and is added to one xmllist, and if not, it is added to another xmllist.

public static function seasonFavHandler(evt:ResultEvent):void {
    Application.application.ManagePage.selectedShow = 
        Application.application.ManagePage.gridFavourites.selectedItem as XML;
    episodeNumber = XML(evt.result).descendants("episode");
    var episode:Object = episodeNumber;
    for each(episode in episodeNumber) {
        currentEpisode = episode as XML;
        achkStatement = new SQLStatement();
        achkStatement.sqlConnection = dbconnection;
        achkStatement.text = "select :episodename from episodes where episodename = :episodename";
        achkStatement.parameters[":episodename"] = episode.title;
        achkStatement.addEventListener(SQLEvent.RESULT, episodeHandler);
        achkStatement.execute();
        trace(episode.title);
    }
   //Application.application.ManagePage.episodeList = episodeNumber;
   seasonHttpService.removeEventListener(ResultEvent.RESULT, seasonFavHandler);
   CursorManager.removeBusyCursor();
}

private static function episodeHandler(event:SQLEvent):void {
    var result:SQLResult = achkStatement.getResult();
    var episodeNewT:XMLList;
    var episodeWatchedT:XMLList;
    if (!result.data) {
        episodeNewT.appendChild(currentEpisode);
        //Application.application.ManagePage.gridUnwatched.addChild(currentEpisode);
    } else {
        episodeWatchedT.appendChild(currentEpisode);
        //Application.application.ManagePage.gridWatched.addChild(currentEpisode);
    }
    Application.application.ManagePage.episodeNew = episodeNewT;
    Application.application.ManagePage.episodeWatched = episodeWatchedT;
    achkStatement.removeEventListener(SQLEvent.RESULT, episodeHandler);
}

      

+2


source to share


3 answers


You have declared episodeNewT

and episodeWatchedT

, but you have not created them yet. I also suggest you use XMLListCollection

instead XMLList

as it is easier to modify at runtime.

Try the following:



var episodeNewT:XMLListCollection = new XMLListCollection();
var episodeWatchedT:XMLListCollection = new XMLListCollection();

if (!result.data) {
    episodeNewT.addItem(currentEpisode);
} else {
    episodeWatchedT.addItem(currentEpisode);
}

Application.application.ManagePage.episodeNew = episodeNewT.copy();
Application.application.ManagePage.episodeWatched = episodeWatchedT.copy();

      

Now your variables will not be empty and you can add them. Note that you will be converting XMLListCollection

back to XMLList

at the end of everything using copy()

.

+3


source


this is my solution for adding xml nodes to xmllist dynamically.



private function addXmlChild(xmlList:XMLList, xmlNode:XML):void
{
    try
    {
        if(xmlList.children().length() != 0)
        {
            xmlList[xmlList.length() + 1] = xmlNode;
        }
        else
            xmlList[0] = xmlNode;               
        } 
        catch(error:Error) 
        {
            LogWriter.ErrorLog("addXmlChild, " + error.message);
        }
}

      

+1


source


But if you want to work with XMLList, you can try this:

var episodeNewT:XMLList;
var episodeWatchedT:XMLList;
if (!result.data) {
    episodeNewT= XMLList(currentEpisode);
    //Application.application.ManagePage.gridUnwatched.addChild(currentEpisode);
} else {
    episodeWatchedT = XMLList(currentEpisode);
    //Application.application.ManagePage.gridWatched.addChild(currentEpisode);
}

      

0


source







All Articles