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


Answer this question

Ambiguos error...

  • Bulldog.NET

    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.



  • sethgold

    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.

  • The Unknown 7

    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 that whether you put 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.



  • JamieWP

    If it is like you are describing, I would love to see that code. Can you email a testcase to me at jvdbeek_13 AT hotmail DOT com

  • TobyKraft

    It's easier to send you a file of frmMain Form because everything is in it.
    I've send you a mail.

    Thanks.

  • AlsoDave

    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 to its 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.



  • jmccall3

    It's little complicated answer then I expected :), but thank you for it.
    Now it makes a perfect sense.

  • JeffOzvold

    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

  • meilon

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

  • Robin Lundgren

    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.

  • johnfrost

    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.



  • Glen Plantz

    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
    }



  • jklegseth

    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.

  • neuroma

    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 or treeView1.TopNode.Nodes.

    Are you confident that the treeview has nodes at all



  • Ambiguos error...