Windows Develop Bookmark and Share   
 index > Windows Forms General > Ambiguos error...
 

Ambiguos error...

Hi all.
I have very strange problem and make me crazy.
I don't know what it is.

This is the code:

int id, n=0;

this.treeView1.TopNode.Nodes.Clear();
for (int i = 0; i < ApplicationState.GroupCount; i++)
{
id =

Convert.ToInt32(xml.xmlData[0].ChildNodes[1].ChildNodesIdea.Attributes["GID"].Value);



this.treeView1.TopNode.Nodes.Add(xml.xmlData[0].ChildNodes[1].ChildNodesIdea.Attributes["GroupTitle"].Value);

for (int j = 0; j < ApplicationState.ItemCount; j++)
{
if (id ==

Convert.ToInt32(xml.xmlData[0].ChildNodes[0].ChildNodes[j].Attributes["GroupID"].Value))
{


this.treeView1.TopNode.NodesNo.Nodes.Add(xml.xmlData[0].ChildNodes[0].ChildNodes[j].InnerText);
this.treeView1.TopNode.NodesNo.Tag =

xml.xmlData[0].ChildNodes[0].ChildNodes[j].Attributes["ID"].Value.ToString();
}
} // for j

this.treeView1.Nodes[0].ExpandAll();
n++;
} // for i

If I put the code in Form load event everything works fine. When I put this code in void and try to run the app, I

get an error: Object reference not set to an instance of an object pointing to

this.treeView1.TopNode.Nodes.Clear();.
The same error is without 'this' keyword.
What Iam doing wrong?
Kasic Slobodan  Thursday, January 26, 2006 6:22 PM

I think you're drawing the wrong conclusions here. I am surprised this code works at all. When filling the treeview for the first time, the TopNode property is always null, causing the exception.

You should always test for null on this property. I would also put Debug.Assert on any pre-conditions in your code. In your case, like this:



Debug.Assert( treeView1 != null );
Debug.Assert( treeView.TopNode != null );

P.s.: you almost got the code tags right... put "code language" instead of language. The endtag is simply code instead of language.

Jelle van der Beek2  Friday, January 27, 2006 6:57 AM

What do you mean by 'put this code in void'?

If you would use this code before InitializeComponent is called, the treeView1 object will be null and so it will throw a null exception. Are you aware of that? The InitializeComponent call is called from the constructor.

Jelle van der Beek2  Thursday, January 26, 2006 6:39 PM
Sorry about that.
I ment: public void BlahBlah(...) - function.

Iam aware of that. I call a function after InitializeComponents and in form load event.
When I put this code i Load event like:

private Form1_Load(...)
{
// Code for reading XML and works great
}


Everything is ok, but if I put a code like this:

public void Something(...)
{
// Same code as above
}

And call this function from Form load event throw me an exception as I wrote in my first post.
This function Something, I call from Form load event:

private Form1_Load(...)
{
Something(); // This thowrs an exception
}

Iam confused...
Kasic Slobodan  Thursday, January 26, 2006 6:55 PM

That makes two of us. :).Can you post the sourcecode? Or at least the constructor code, the Load code and the method that throws the exception?

Do you know which object is null? From this line:



this.treeView1.TopNode.Nodes.Clear();.

.. it could be treeView1, treeView1.TopNode ortreeView1.TopNode.Nodes.

Are you confident that the treeview has nodes at all?

Jelle van der Beek2  Thursday, January 26, 2006 7:41 PM
This is the constructor code

[language="c#"]
public frmMain()
{
InitializeComponent();
FillTreeFromXml();
}
[/language]

Iam doing this because I want this function to run for the first time just once.
On For Activate I run this function again if appropriate button was pressed so that treeView can be refreshed.

[language="c#"]
private void frmMain_Activated(object sender, System.EventArgs e)
{
if (ApplicationState.AlredyClicked)
{
FillTreeFromXml();
}
}
[/language]

Error is the one I mentioned in my first post...

The problem with this Clear() stuff is that this same code works if this code isn't in any function, but in Form Load event directly (so this means that isn't matter if there's any nodes or not :S). This moment I put it inside the function and compile the app (no error there) and start the app, error raises pointing this Clear() stuff :S.
If I comment that Clear(), error raises on next occurence of treeView1...

I can solve this problem by copying entaire code twice (in constructor and Form Activated event, but I wan't do like this).
Now Iam stubborn and I don't want to do this, but with functions because I know that it can be done, but whats the problem, beats me.
Kasic Slobodan  Friday, January 27, 2006 2:05 AM

I think you're drawing the wrong conclusions here. I am surprised this code works at all. When filling the treeview for the first time, the TopNode property is always null, causing the exception.

You should always test for null on this property. I would also put Debug.Assert on any pre-conditions in your code. In your case, like this:



Debug.Assert( treeView1 != null );
Debug.Assert( treeView.TopNode != null );

P.s.: you almost got the code tags right... put "code language" instead of language. The endtag is simply code instead of language.

Jelle van der Beek2  Friday, January 27, 2006 6:57 AM
About that language tag, I couldnt find anywhere the right syntax. Few months ago there were options on editor for VB & C# code, but now I can't see 'em.
When I tried to edit my post, all I get is an empty textarea (where text suppost to be).
Thank you for your help about language tags. Ill keep in mind that.

Let me go back to the problem.
I forgot to tell you that I have added topnode in design mode so that topnode can't be null.
I will try this code you sugested to me and see if any of this methods are null.


This code works great:



private void frmMain_Load(object sender, System.EventArgs e)
{
int id, n=0;

// brisanje svih objekata u kontroli
this.treeView1.TopNode.Nodes.Clear();
for (int i=0; i < ApplicationState.GroupCount; i++)
{
id = Convert.ToInt32(xml.xmlData[0].ChildNodes[1].ChildNodesIdea.Attributes["GID"].Value);

this.treeView1.TopNode.Nodes.Add(xml.xmlData[0].ChildNodes[1].ChildNodesIdea.Attributes["GroupTitle"].Value);

for (int j=0; j < ApplicationState.ItemCount; j++)
{
if (id == Convert.ToInt32(xml.xmlData[0].ChildNodes[0].ChildNodes[j].Attributes["GroupID"].Value))
{
this.treeView1.TopNode.NodesNo.Nodes.Add(xml.xmlData[0].ChildNodes[0].ChildNodes[j].InnerText); // dodajemo naslov Code-a u child parent-a #2
this.treeView1.TopNode.NodesNo.Tag = xml.xmlData[0].ChildNodes[0].ChildNodes[j].Attributes["ID"].Value.ToString(); // dodajemo naslov Code-a u child parent-a #2
}
} // for j

this.treeView1.Nodes[0].ExpandAll();
n++;
} // for i
}



But this code throws an exception



private void frmMain_Load(object sender, System.EventArgs e)
{
FillTreeFromXml(); // Where the body of this function is code from Load event
}




Same code just different calls :S.
Ill try with Debug.Assert. Thank you for sugestion.
Kasic Slobodan  Friday, January 27, 2006 3:26 PM
If it is like you are describing, I would love to see that code. Can you emaila testcase tome at jvdbeek_13 AT hotmail DOT com?
Jelle van der Beek2  Friday, January 27, 2006 3:40 PM
Now Iam more confused. Code now works.
Your sugestion about Debug.Assert helped me. Thank you for that.

Iam pretty sure that app couldn't start if I've called a function from Form Load event.

I had function call in Forms consrtuctor and in Form Activated event.
Debug.Assert helped me by pointing to Main method where is created this form so I figure out that might be a problem in forms constructor (where I call that function) so I've commented that, put the function call in Form Load event run the app and everything was OK .

Now I can't remmember was the app worked when I've put the function in Form Load event, but Iam pretty sure that doesen't (or there was something in Forms constructor in that time).
Now Iam wondering why function wan't work when I call it after InitializeComponents?

Can you tell me what is testcase?
I can send you this code anyway if you want.

And thank you for your time spending on solving my problem.
I appreciate that.

Conclusion:
The problem was because I've called a function from forms constructor. When I've called a function from Form Load event, everything was fine.

Now I can't stop thinking about how this function wasn't worked before when I've called it from Form Load event.

Thank you again.
Wich post to highligh as right answer?
Kasic Slobodan  Friday, January 27, 2006 6:38 PM

You must have made a simple mistake somewhere, but it is hard to figure it out now if you don't have the precise code anymore.

With a testcase I meant an isolated piece of code that shows the bug. Instead of sending a complete project with a lot of files, it is often handy to strip the project down toits bare necessities. But if your problem is fixed you don't need to send me the code.

Just mark the answer that pointed you to the right solution as answer. Just pick one so the thread is marked as answered.

Jelle van der Beek2  Friday, January 27, 2006 6:48 PM
Thank you again.

Iam curious. Why the functions didn't worked when it was called from Forms constructor?



public frmMain()
{
InitializeComponent(); // Every control has created and treeView also
FillTreeFromXml(); // Throws an exception
}


Kasic Slobodan  Friday, January 27, 2006 7:37 PM
I just don't know. If you have a project that throws the exception you can send it to me by email and I will try to check it out.
Jelle van der Beek2  Friday, January 27, 2006 7:40 PM
It's easier to send you a file of frmMain Form because everything is in it.
I've send you a mail.

Thanks.
Kasic Slobodan  Friday, January 27, 2006 8:08 PM

Ok... I think I have the final answer for you here.

TreeView.TopNode returns the first visible node. Internally it sends the Win32 message TVM_GETNEXTITEM to the treeview with the TVGN_FIRSTVISIBLE flag.

I assume this also means that if the treeview is not yet visible, TopNode will always be null. You must have been mistaken thatwhether youput it in a method or not the behaviour changed. The treeview is not visible in the constructor yet, so TopNode will always return null.

If you put it in the Load event instead, the treeview is visible and TopNode returns the correct first visible node. For safety, I would always check if TopNode is null.

Jelle van der Beek2  Tuesday, January 31, 2006 1:56 PM
It's little complicated answer then I expected :), but thank you for it.
Now it makes a perfect sense.
Kasic Slobodan  Wednesday, February 01, 2006 2:09 AM

You can use google to search for other answers

Custom Search

More Threads

• how to allign string with spaces
• caption bar design
• Dispose not being called on some designer generated control instances
• How to trap the keydown or key pressed in a combo box
• Performance of FillRectangle() and Clear()
• Windows properties??
• how to make use of listbox itemdata property in vb.net window application
• How activate a ChildForm, with a menuItem?
• Lines? Boxes? Gone...
• help needed