[英]NullReference exception occurs even when the object is not null
I am new to wpf; 我是wpf的新手。 I am using an editable comboBox (for search purposes). 我正在使用可编辑的comboBox(出于搜索目的)。
When text in the ComboBox is changed, the search result is displayed underneath the datagrid. 更改组合框中的文本后,搜索结果将显示在数据网格下方。 When any row from the datagrid is selected its values are displayed in textboxes for editing. 从数据网格中选择任何行后,其值将显示在文本框中以进行编辑。
When I write something in the combobox, the related row is displayed in the data grid, but when I click to select a row, the application throws a nullreference exception
. 当我在组合框中编写内容时,相关行显示在数据网格中,但是当我单击以选择一行时,应用程序将引发nullreference exception
。
My application worked correctly when the dataGrid refreshing logic was behind a button click. 当单击按钮后出现dataGrid刷新逻辑时,我的应用程序正常工作。
The code for "SelectionChange" Event of the dataGrid is: dataGrid的“ SelectionChange”事件的代码为:
private void CategoryRowSelected(object sender, System.Windows.Controls.SelectedCellsChangedEventArgs e)
{
ClearForm();
if(CategoryDataGrid.SelectedItem!=null)
{
categoryMember = CategoryDataGrid.SelectedItem as CategoryTbl; // value assigned to the object
// if (categoryMember != null)
CategoryName.Text = categoryMember.CategoryName; //Exception thrown on this statement
CategoryDescription.Text = categoryMember.CategoryDescription;
}
}
and code for the textChange event of ComboBox is: ComboBox的textChange事件的代码是:
private void RefreshDataGrid(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
CategoryDataGrid.SelectedIndex = -1;
//CategoryDataGrid.ItemsSource = RefreshQuery;
CategoryDataGrid.ItemsSource= Admin.RefreshCategoryDataGrid(NameCombo.Text);
}
CategoryName.Text = categoryMember.CategoryName; //Exception thrown on this statement
This can happen for multiple reasons - not just because categoryMember
is null. 发生这种情况有多种原因-不仅是因为categoryMember
为null。 It will also occur if: 在以下情况下也会发生:
categoryMember.CategoryName
(the CategoryName
property itself) returns null
, as TextBox.Text
and similar properties raise the exception if you set the value to null
. categoryMember.CategoryName
( CategoryName
属性本身)返回null
,因为如果将TextBox.Text
和类似的属性的值设置为null
则会引发异常。 CategoryName
(the control) is null
CategoryName
(控件)为null
Also, I see you had a null
check (for debugging?), but it is commented out. 另外,我看到您有一个null
检查(用于调试?),但已将其注释掉。 If CategoryDataGrid.SelectedItem
is not assignable to CategoryTbl
, you will receive null in categoryMember
itself. 如果CategoryDataGrid.SelectedItem
是不能分配给CategoryTbl
,您会收到在空categoryMember
本身。
in addition to @Reed answer would say, that considering that you say that on Button
click it works, I immagine Button
was on the cell. 除了@Reed回答之外,考虑到您说在Button
单击时它起作用,我想像Button
在单元格上。 In this case returned type is a different then may happen in CategoryDataGrid.SelectedItem
. 在这种情况下,返回的类型是不同的,然后可能发生在CategoryDataGrid.SelectedItem
。 Most probabbly CategoryDataGrid.SelectedItem
is a container of a some type and not directly of a type CategoryTbl
最有可能的CategoryDataGrid.SelectedItem
是某种类型的容器 ,而不是直接类型为CategoryTbl
的容器
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.