Darren Young
Darren Young

Reputation: 11100

C# Object reference not set to an instance of an object

I have the following code:

panel4.Controls.Clear();
        panel4.Controls.Add(dgv.addDatagrid(animal, experiment, pluginIdentifier));
        dgv.ConfigureDatagrid();

This gets called when a user clicks a node on a treeview. When I click on it the first time, it works as expected and obtains the datagrid and adds it to the panel. However, any subsequent calls, I get the 'Object reference not set to an instance of an object' error on the panel4.Controls.Clear() line. Anybody know why?

Thanks.

Upvotes: 0

Views: 742

Answers (3)

Sony Mathew
Sony Mathew

Reputation: 57

The error happens becoze of any field's value becomes null. Debug it where the Controlls.Add function is calling and see any parameter has null value

Upvotes: 0

Jack Allan
Jack Allan

Reputation: 15014

Just guessing based on your code... It could either be that panel4 is null or panel4.Controls is null. Take a look at where and how panel4 is used and make sure it's correctly initialized

Upvotes: 0

WraithNath
WraithNath

Reputation: 18013

If you put a breakpoint on panel4.Controls.Clear(); and check for null on all controls the second time the breakpoint is hit you should be able to see which one is throwing the error.

its likely to be panel4 or dgv

Upvotes: 2

Related Questions