views:

1095

answers:

2

Hi Everyone,

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);
}
A: 

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().

Matt Dillard
This seems to have fixed the NULL issue, but now i get another error about the list being empty. TypeError: Error #1086: The appendChild method only works on lists containing one item.
medoix
You're right. If you look at the Flex docs for XMLList, you'll notice there is no appendChild() method. The XML datatype DOES have an appendChild() method, however. What's happening here is that Flex is trying to help you out - the compiler will let you make calls against an XMLList as though you were talking to just a single XML element, but that only works at runtime if there is indeed just one element in the list.Instead, just use an XMLListCollection, which provides better support anyway.
Matt Dillard
Legend! thank you very much. I have another issue now but i do understand why and i will work on that separate.
medoix
A: 

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);
}
vmaksym