.NET winform - Checkbox nullReferenceException

时间:2014-12-12 09:43:18

标签: c# .net winforms checkbox

当我试图设置checkbox.checked时,我得到了null refedence异常。我不知道为什么,看起来这个复选框没有初始化。

有我的代码

public UpdateProduct(DataGridViewRow row,Form1 form)
{
    this.row = row;
    this.form = form;
    product = row.DataBoundItem as object;
    visibilityCheckBox.Checked = true;
    InitializeComponent();
}

异常是在visibilityCheckBox.Checked = true;

行中触发

任何人都可以帮我解决这个问题吗?非常感谢 !

1 个答案:

答案 0 :(得分:3)

我猜您之前没有致电InitializeComponent,因为您在调用InitializeComponent之前尝试设置它,visibilityCheckBox将为null

首先调用InitializeComponent或编写另一个只更新创建的UI元素的方法。