none
Object reference not set to an instance of an object - i get that whenever I try to add a new node

    Question

  • I get Object reference not set to an instance of an object whenever I try to add a new node to my treeview on the click of a button. My code is as follows:

            private void bNewSite_Click(object sender, EventArgs e)
            {
                bNewBookmark.Enabled = true;
                bRename.Enabled = true;
                bDelete.Enabled = true;
                bDuplicate.Enabled = true;
                tabControl1.Enabled = true;
    
                TreeNode newNode = new TreeNode("New Connection");
                treeEntry.SelectedNode.Nodes.Add(newNode);
            }

    Any and all help or input will be greatly appreciated. Thanks in advance

    Friday, September 06, 2013 4:32 PM

Answers

  • Hi Timothy,

    According to your description of this issue, I think the SelectedNode instance is null, so it throw object reference not set to an instance of an object error. Per my understanding, you want to add a treenode to a SelectedNode. Please check that you have selected a treenode first before click the button.

    I write some code for you to try:

    //check the instance is null

                if (treeEntry.SelectedNode == null)

                {

                    MessageBox.Show("tips");

                    return;

                }

                bNewBookmark.Enabled = true;

                bRename.Enabled = true;

                bDelete.Enabled = true;

                bDuplicate.Enabled = true;

                tabControl1.Enabled = true;

                TreeNode newNode = new TreeNode("New Connection");

                treeEntry.SelectedNode.Nodes.Add(newNode);

    Best Regards,


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, September 10, 2013 2:55 AM
    Moderator
  • The exception will tell you where the problem is occurring.  Looking at your code I'd wager SelectedNode is null because a tree doesn't necessarily have a selected node.  Your button should probably be disabled if no node is selected.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Friday, September 06, 2013 4:48 PM
    Moderator

All replies

  • The exception will tell you where the problem is occurring.  Looking at your code I'd wager SelectedNode is null because a tree doesn't necessarily have a selected node.  Your button should probably be disabled if no node is selected.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Friday, September 06, 2013 4:48 PM
    Moderator
  • <copied>

    Object reference not set to an instance of an object

    <end>

    example.......

    private Timothy tim = new Timothy()

    tim is an instance of Timothy and tim is in memory.

    string name = tim.name;     // no problem because tim exist in memory and tim can be addressed/referenced in code.

    private Timothy tim

    tim was declared but tim is not a "new" instance of Timothy and tim is not in memory.

    string name = tim.name    // and boom it going to blow up, because tim is not in memory, it is a null value it was never made and you will get the error message below.

    Object reference not set to an instance of an object

    The above is just an example of what the error message means, which is the object is not there and it can't be referenced.

    It doesn't matter of it is .NET or Java, because the principles of Object Oriented Programming are the same.

    http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Friday, September 06, 2013 7:36 PM
  • Hi Timothy,

    According to your description of this issue, I think the SelectedNode instance is null, so it throw object reference not set to an instance of an object error. Per my understanding, you want to add a treenode to a SelectedNode. Please check that you have selected a treenode first before click the button.

    I write some code for you to try:

    //check the instance is null

                if (treeEntry.SelectedNode == null)

                {

                    MessageBox.Show("tips");

                    return;

                }

                bNewBookmark.Enabled = true;

                bRename.Enabled = true;

                bDelete.Enabled = true;

                bDuplicate.Enabled = true;

                tabControl1.Enabled = true;

                TreeNode newNode = new TreeNode("New Connection");

                treeEntry.SelectedNode.Nodes.Add(newNode);

    Best Regards,


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, September 10, 2013 2:55 AM
    Moderator