Windows Develop Bookmark and Share   
 index > Windows Forms General > TreeView.Invoke Bug - NullPointerException
 

TreeView.Invoke Bug - NullPointerException

Hi!

I have a class that derives from TreeNode. I've got a wierd NullPointerException from the Invoke method of the TreeView until I discovered that I had to add "new object()" to the Invoke call. Here is my code:

Code Block

private void MyMethod()
{
if (TreeView.InvokeRequired)
TreeView.Invoke(new WaitCallback(InternalLoadHandlerContentsUI), new object());
else
InternalLoadHandlerContentsUI(null);
}

private void InternalLoadHandlerContentsUI(object obj)
{
// here I am!
}



If I remove the "new object()" parameter, the Invoke method throws a NullPointerException. If I add the parameter, the delegate is called correctly.

I just wrote this if someone has the same troubles! Wink

Regards
confused1982  Friday, December 14, 2007 8:39 AM

Hi, confused1982,

Based on my understanding, when you invoke a method of your TreeView without the paramter part, you will get a NullPointerException, right?

Well,I am afraid that this is not a bug/issue with TreeView.

Because your InternalLoadHandlerContentsUI method has an object parameter, so you will need to pass a parameter to the delegate.

Please read this article

http://msdn2.microsoft.com/en-us/library/a1hetckb(VS.80).aspx

"method

A delegate to a method that takes parameters of the same number and type that are contained in the args parameter. "

As you are using aWaitCallBack to create the delegate, if you remove the new object(), it will lose the parameter and give you a NullPointerException.

Maybe this sample will give you a more clear concept.

Code Block

object nullObject = null;

private void MyMethod()

{

if (treeView1.InvokeRequired)

{

treeView1.Invoke(new WaitCallback(LoadHander), nullObject);

}

//......

}

Hi, confused1982,

Based on my understanding, when you invoke a method of your TreeView without the paramter part, you will get a NullPointerException, right?

Well,I am afraid that this is not a bug/issue with TreeView.

Because your InternalLoadHandlerContentsUI method has an object parameter, so you will need to pass a parameter to the delegate.

Please read this article

http://msdn2.microsoft.com/en-us/library/a1hetckb(VS.80).aspx

"method

A delegate to a method that takes parameters of the same number and type that are contained in the args parameter. "

As you are using aWaitCallBack to create the delegate, if you remove the new object(), it will lose the parameter and give you a NullPointerException.

Maybe this sample will give you a more clear concept.

Code Block

object nullObject = null;

private void MyMethod()

{

if (treeView1.InvokeRequired)

{

treeView1.Invoke(new WaitCallback(LoadHander), nullObject);

}

//......

}

You can use google to search for other answers

Custom Search

More Threads

• how to save the content of richTextBox as jpg file?
• Is it possible to find the Event occurring for a particular control?
• TabControl left or right -- text not visible
• Error result returned from Resgen.exe
• dynamic pictureBox creation
• another System.Windows.Forms.HScrollBar question
• "A first chance excep typ 'System.InvalidOperationException' occurred in System.Windows.Forms.dll COM" problem (THREADS)?
• Confirmation
• Graphics: How to create smooth Clip region?
• Problems hosting a custom control / cell / column in a DataGridView control