tags:

views:

1099

answers:

3
A: 

Have you tried debugging this in flex builder or eclipse?

The most likely candidate for your null reference error is the 'item' argument passed to namer(). The tree component does pass a string through to the labelFuntion method but it may not be a string that can be converted to an xml object. I'd definitely check that first.

The other thing that I can see is wrong is that you are accessing localName as if it's a property. It's actually method so you should be calling it by nodeName.localName(). Edit... this is incorrect now as i didn't realise nodeName was of type QName. localName is actually a property of this type. appologies

Edit: also your Tree component has a dataProvider of

dataProvider="{treeData}"

From the example you have given it doesn't look like treeData exists. Should that be fullXML?

James Hay
A: 

According to Flex and the guy who answered my other question (Herreman) calling localname as a property is the right way to do things.

fullXML and that stringtest line are artifacts. I have the XML embedded in the same file atm.

It fails on return nodeName.localName when it is trying to draw the leaf nodes. SO I know it has something to do with:

return nodeName.localName;

and

public function treeChanged(event:Event):void {
selectedNode=Tree(event.target).selectedItem;
}
mvrak
local name is a method. should be "return nodeName.localName();" Not sure if that is the only problem or not.
invertedSpear
+1  A: 

OK. It sounds like your XML looks something like this:

<root>
  <test>
    <child>leaf 1</child>
  </test>
  <test2>
    <child2>leaf 2</child2>
  </test2>
</root>

The significant part of this is that there is simple content within the child and child2 tags. Expanding the tree to show 'leaf 1' or 'leaf 2' causes the error you are receiving, because node.name() will return null. This makes sense, because 'leaf 1' and 'leaf 2' are text nodes and don't have node names.

To correct the problem, you can update the namer function to something along these lines:

public function namer(item:Object):String {
    var node:XML = XML(item);
    var nodeName:QName = node.name();
    if (nodeName) {
        return nodeName.localName;
    } else {
        return String(node);
    }
}

This will use 'leaf 1' and 'leaf 2' as the label for the corresponding nodes in the tree.