AfterSelect event error

J

juli jul

Hello,
I have a treeview and an AfterSelect event ehich is defined in the
InitializeComponenet() function this way:

this.treeView1.AfterSelect += new
System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);

(I also tried to define it in the Form_Load() but it didn't help)
I also have funcetion which filles the tree elements (fills them
properly)
The problem is that after a number of clicks on the tree view elements I
get this error:
An unhandled exception of type 'System.NullReferenceException' occurred
in Unknown Module.

Additional information: Object reference not set to an instance of an
object.

Why? What is wrong here?
Thank you!
 
C

Claudio Grazioli

I have a treeview and an AfterSelect event ehich is defined in the
InitializeComponenet() function this way:

this.treeView1.AfterSelect += new
System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);

(I also tried to define it in the Form_Load() but it didn't help)
I also have funcetion which filles the tree elements (fills them
properly)
The problem is that after a number of clicks on the tree view elements I
get this error:
An unhandled exception of type 'System.NullReferenceException' occurred
in Unknown Module.

Additional information: Object reference not set to an instance of an
object.

Why? What is wrong here?

Please post the code of treeView1_AfterSelect method.

I assume you only get the exception when using this event?
 
J

juli jul

I erased all the code inside of it and the error keeps showing up - so
now there is no code there and it happens only after selecting the nodes
in a tree view couple of times. Why?
Thanks!
 
C

Claudio Grazioli

I erased all the code inside of it and the error keeps showing up - so
now there is no code there and it happens only after selecting the nodes
in a tree view couple of times. Why?
Thanks!

And when you remove the event handler? It doesn't crash anymore?
 
J

juli jul

I think I know what caused it but I don't know why,I have a function
which adds xml nodes to a tree:
private void populateTreeControl(
System.Xml.XmlNode document,
System.Windows.Forms.TreeNodeCollection nodes)
{
XmlNodeList xmn = document.SelectNodes("*");
foreach (System.Xml.XmlNode node in
xmn)
{
string text = (node.Value != null ? node.Value :
(node.Attributes != null &&
node.Attributes.Count > 0) ?
node.Attributes[0].Value : node.Name);
TreeNode new_child = new TreeNode(text);
nodes.Add(new_child);
populateTreeControl(node, new_child.Nodes);
this.writeXml();

I changed it a liite (here is the change):
string text;
if(node.Value!=null)
{
text=node.Value;
}
else
{
if(node.Attributes.Count==0)
{
text=node.Name;
}
else
return;

I think that this is reason for the crash and I don't understand why?
Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top