0

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.

3
  • 1
    Something in your code is killing panel4, as Controls will never be null afaik.. start by posting code of ConfigureDatagrid method. Another option is that you got the wrong line and maybe dgv is null. Commented Jan 31, 2011 at 10:55
  • Could you add some more code? Obviously panel4 is reset somewhere. Commented Jan 31, 2011 at 10:56
  • Wow, amazing how many related questions share the same title... Nulls suck. Commented Jan 31, 2011 at 11:10

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

How can I check for nulls on all controls? Sorry if this is a silly question....I'm fairly new on debugging. Thanks.
When the breakpoint is hit and goes yellow you should be able to hover over the variable / control names to see the value
@Darren: When the debugger breaks on that line, just hover with the mouse over the text 'panel4'. You will see a small box popup showing the value of that variable. Alternatively, you can type 'panel4' into the Watch window, or type '?panel4' in the Immediate window.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.