Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I'm currently stuck with a problem.
I tried customizing a treeview in C# .Net 3.5.
I owner drawed the nodes.
But an exception occurs frequently while performing the following steps:
1. Add 2 nodes with large text to the treeview.
2. Set the RightToLeft and RightToLeftLayout properties to true.
3. Remove all nodes using RemoveAll() API.
4. Add 2-3 nodes with large text in the tree view.

A null reference exception occurs. The stack trace is as follows:
at System.Windows.Forms.TreeNode.get_Handle()
at System.Windows.Forms.TreeNode.get_RowBounds()
at System.Windows.Forms.TreeView.CustomDraw(Message& m)
at System.Windows.Forms.TreeView.WmNotify(Message& m)
at System.Windows.Forms.TreeView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
The message send to the WndProc during the exception is "WndProc(ref System.Windows.Forms.Message message = {msg=0x204e (WM_REFLECT + WM_NOTIFY))"

Kindly help me in solving the issue.
Posted
Updated 4-Jan-18 4:53am
Comments
Sergey Alexandrovich Kryukov 5-Aug-13 10:41am    
Exception stack trace without the code which throws exception? Hm? How about thinking on the situation a bit?..
—SA

Despite of showing some exception stack trace, you did not show where this 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
 
Comments
aravindkrgec 6-Aug-13 1:43am    
Thanks for replying.
The exception was thrown on WndProc's default case( Message that is not custom handled).
So debugging does not help.
Sergey Alexandrovich Kryukov 6-Aug-13 11:35am    
How "debugging does not help"? What can help then? adding bugs? :-)
—SA
Kaizen202 7-Aug-13 1:13am    
@Sergey:
From the stack trace, could understand that the null reference exception is thrown when handling "{msg=0x204e (WM_REFLECT + WM_NOTIFY))" message in WndProc() method, and it is exactly from "System.Windows.Forms.TreeNode.get_Handle()" method.
So it difficult to debug since the exception is from base control.
@Aravind (FunCorner):
Are yo handling "WM_NOTIFY" message correclty?
Sergey Alexandrovich Kryukov 7-Aug-13 1:48am    
The problem is: the stack trace you show does not show a single line of your code, but such code should be in trace.
—SA
The issue was with Reflect Notify message, WM_CustomDraw.
Due to some reasons, the handle for the last node was not created at the time of drawing.
So validated that and all went well.

Thanks for your support in this matter.

Regards
Aravind
 
Share this answer
 

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