tags:

views:

37

answers:

1

Hi All.

I have a problem creating MenuItems for a TreeView dynamically: here is the (simplified)code i'm using.

public class CTMProvider
{
    private CTM myObject;
    //internal CTMManager manager;
    //internal string IEEEAddress;
    //internal System.Net.Endpoint endpoint;

    public CTMProvider()
    {
        myObject = new CTM(this);
    }

    void Disconnect(object sender, EventArgs ea) {
        //Function disconnects a tcp link
    }
}

public class CTM : System.Windows.Forms.TreeNode
{
    public CTM(CTMProvider provider)
    {
        //this.provider = provider;
        //this.manager = provider.manager;
        //this.IEEEAddress = provider.IEEEAddress;
        //this.endpoint = provider.state._conn.RemoteEndPoint;
        this.Text = String.Format("CTM: {0} {0}", IEEEAddress, ((System.Net.IPEndPoint)endpoint).ToString());
        MenuItem meni = new MenuItem("Disconnect", new System.EventHandler(this.provider.Disconnect)));
        this.ContextMenu.MenuItems.Add(meni);  // <-----
    }
}

This code always triggers a NullReferenceException when i try and add my menuitem to the MenuItems list. Any Ideas?

+1  A: 

In Visual Studio go to "Debug->Exceptions..." (or Ctrl+Alt+E) and check Common Language Runtime Exceptions checkbox under Thrown column. The exception now will be shown in exact place it happened, not just in catch block.

Oleg I.