Question

I am trying to parse some XML i have retrieved via e4x in an HTTPService. The loop works and for each episode in the list it goes through the loop. However i get the following error when it is trying to append to an XMLList.

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

I am trying to query the local SQLite database and see if the episode exists (working) and if it does append to one xmllist and if not then append to the other 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);
}
Was it helpful?

Solution

You have declared episodeNewT and episodeWatchedT, but you haven't instantiated them yet. I would also suggest you use XMLListCollection instead of XMLList, since it's easier to modify at runtime.

Try this:

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 won't be null, and you can append to them. Note that you'll be converting the XMLListCollection back to an XMLList at the end of it all by using copy().

OTHER TIPS

this is my solution to add 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);
        }
}

But if you want work with XMLList, you can try the next:

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top