简体   繁体   中英

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM