当对象不为null时抛出NullReference [英] NullReference thrown when the object is not null

查看:93
本文介绍了当对象不为null时抛出NullReference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是wpf的新手,我使用可编辑的comboBox(用于搜索).更改ComboBox中的文本时,下面的datagrid将显示搜索结果.当从数据网格中选择任何行时,其值将显示在文本框中以进行编辑.
当我在组合框中编写内容时,已实现的行显示在数据网格中..但是当我单击以选择一行时,应用程序将引发nullreference异常.
当单击按钮后出现dataGrid刷新逻辑时,我的应用程序正常工作.

dataGrid的"SelectionChange"事件的代码为:

I am new to wpf, i m using a editable comboBox (for search purpose). When text in ComboBox is changed, below datagrid displays the search result. when any row from the datagrid is selected its values are displayed in textboxes for editing.
when i write something in combobox the realated row are displayed in the data grid.. but when i, click to select a row, application throws a nullreference exception.
My application worked correctly when the dataGrid refreshing logic was behind a button click.

the code for "SelectionChange" Event of the dataGrid is:

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:

private void RefreshDataGrid(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            CategoryDataGrid.SelectedIndex = -1;           
            //CategoryDataGrid.ItemsSource = RefreshQuery;
            CategoryDataGrid.ItemsSource= Admin.RefreshCategoryDataGrid(NameCombo.Text);
        }



我希望很快得到答案....



I hope to get an answer sooon....

推荐答案

可能性非常明显:
1)categoryMember为null,或者是因为CategoryDataGrid.SelectedItem为null,或者不是CategoryTbl,

2)CategoryName为空.

在处理程序的第一行上放置一个断点,并逐步执行,直到找出哪一个为止.然后,您可以修复它,或让我们知道(并显示相关代码),然后我们来看一下.
The possibilities are pretty obvious:
1) categoryMember is null, either because CategoryDataGrid.SelectedItem is null, or is not a CategoryTbl,
or
2) CategoryName is null.

Put a breakpoint on the first line of the handler, and step through until you find out which. Then you can either fix it, or let us know (and show the relevant code) and we can have a look.


这篇关于当对象不为null时抛出NullReference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆