Click here to Skip to main content
15,889,654 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have windows application that manipulates treeview; on select of a node from the treeview it gets the selected node. The code looks

C#
private MethodInfo[] methodInfo;
private ParameterInfo[] param;

private void treeview1_AfterSelect(object sender, TreeViewEventArgs e)
 {
        ….
    param = methodInfo[e.Node.Index].GetParameters();

        …..
}

Now I want to implement it using ASP.NET, and on select event of a node from treeview the code looks

C#
 protected void treeview1_SelectedNodeChanged(object sender, EventArgs e)
{
        ….
    
param = methodInfo[treMethods.SelectedNode.Depth].GetParameters();

        …..
}


And it returns error that says “Object reference not set to an instance of an object”. In Asp.net EventArgs didn’t have features like Node. I have tried to use TreeNodeEventArgs instead of EventArgs but it didn’t allow me to override SelectedNodeChanged method.
Posted
Updated 12-Feb-15 3:54am
v2
Comments
Herman<T>.Instance 12-Feb-15 9:55am    
Did the debug tell you which object is NULL?
getrelax 12-Feb-15 10:21am    
treMethods.SelectedNode.Depth is returning null reference. But if I am able to use TreeNodeEventArgs (i.e instead of EventArgs ) I can use e.Node.Index.
Richard Deeming 12-Feb-15 12:44pm    
The SelectedNodeChanged event is declared as an EventHandler, not a TreeNodeEventHandler. You can't just change the type of the EventArgs argument and expect the event to magically give you extra information.

Your event handler is called treeview1_SelectedNodeChanged, but refers to a control called treMethods. Did you change the name after adding the event, or are you looking at the wrong TreeView control?
getrelax 13-Feb-15 3:14am    
Actually the name of the eventhadnler is treMethods_SelectedNodeChanged and the treeview control is treMethods, I changed the name of the event handler in this post just to create similarity between the windows and web version of the code. So I am not looking in the wrong control. Unlike TreeNodeEventArgs the EventArgs doesn’t have Node property, is there any other alternative?
Richard Deeming 13-Feb-15 8:24am    
No, you're stuck with checking the SelectedNode property. You just need to check whether it returns null before you try to access its Depth property.

Also, you need to bear in mind that the Depth property on the node doesn't return the same thing as the Index property on the WinForms version. Depending on how the tree is constructed, you might need to look for an alternative - for example, the Value property.

To start with, TreeViewEventArgs is System.Windows.Forms.TreeViewEventArgs. It is totally unrelated to ASP.NET, so just forget it. Formally, this is the answer to your question: there is no use.

Now, there is no such thing as "returns error". Nothing "returns error" in modern programming. There are exceptions, they never returned. You need to learn and understand structural exception handling, without it, there is nowhere to go. See also my past answers:
Does Exception in C# Constructor Cause Caller Assignment to Fail?[^],
where was stored .net exceptions in operating system[^].

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 
I didn’t say TreeViewEventArgs, I said TreeNodeEventArgs which is System.Web.UI.WebControls.TreeNodeEventArgs. As I mentioned in the question the first part of the code shows the windows implementation and the second part is web implementation. And my question is how can I use TreeNodeEventArgs instead of EventArg for the web application?
 
Share this answer
 
Comments
Richard Deeming 12-Feb-15 12:40pm    
If you have a question or comment, use the "Have a Question or Comment?" button under the relevant solution; don't post it as a new solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900