NullReference异常由用户代码取消了解。 [英] NullReference Exception was Unhandeled byuser code.

查看:77
本文介绍了NullReference异常由用户代码取消了解。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在下面的代码中使用以下行获得了上述错误。

对象引用没有设置为Object的实例。



Hi i'm getting the above error with following line in my below code.
"Object refrence not set to the instance of the Object."

DepartmentID.Text = Asset.mASS_mDEP_NUPKId_Department.ToString();
if (DepartmentID.Text != "0")
{
  LQMasterComDepartmentDataContext CDbde = new LQMasterComDepartmentDataContext();
  TB_MasterCompanyDepartment AssDepartment = CDbde.TB_MasterCompanyDepartments.FirstOrDefault
  (C => C.mCDP_NUPKId == mobjGenlib.ConvertLong(DepartmentID.Text));
  DepartmentName.Text = AssDepartment.mCDP_VCDepartmentName;
}





我收到以下错误



I'm getting error in below line
"

DepartmentName.Text = AssDepartment.mCDP_VCDepartmentName;







可以任何一个请帮忙我想出来了吗?



提前谢谢。

"


can any one please help me to figure it out?

Thanks in advance.

推荐答案

有趣的变量名称:笑:<无论如何,你得到NullReferenceException的原因可能在于前一行:

Interesting variable name :laugh:

Anyway, the reason you're getting a NullReferenceException probably lies in the previous line:
TB_MasterCompanyDepartment AssDepartment = CDbde.TB_MasterCompanyDepartments.FirstOrDefault(C => C.mCDP_NUPKId == mobjGenlib.ConvertLong(DepartmentID.Text));





TB_MasterCompanyDepartments 是一个空集合,或 DepartmentID.Text 与任何 mCDP_NUPKId 值不匹配。



使用调试器逐步执行代码,很容易发现问题所在。无论哪种方式,它都是 AssDepartment 完全有效,所以测试它并相应地处理它。



Either TB_MasterCompanyDepartments is an empty collection, or the value of DepartmentID.Text does not match any mCDP_NUPKId value.

Step through your code with a debugger and it's pretty easy to spot what the problem is here. And either way it's entirely valid for AssDepartment to be null, so test it and handle it accordingly.


首先,是实体' DeparmentName除了'null这里以外的其他东西?



在该行上放置一个断点:DepartmentName.Text = AssDepartment.mCDP_VCDepartmentName;



当你到达那个断点时,检查'DepartmentName及其'Text属性的值。



如果'DepartName没问题,然后打开命令窗口... Control + W,A ...并输入:? AssDepartment< return> :



检查命令窗口中的输出:是'AssDepartment你认为它应该是什么;属性'mCDP_VCDepartmentName看起来是否正确?
First, is the entity 'DeparmentName something other than 'null here ?

Put a break-point on the line: DepartmentName.Text = AssDepartment.mCDP_VCDepartmentName;

When you reach that break-point, inspect the value of 'DepartmentName, and its 'Text property.

If 'DepartName is okay, then open the command-window ... Control+W,A ... and type: ? AssDepartment <return> :

Examine the output in the command-window: is 'AssDepartment what you think it should be; does the property 'mCDP_VCDepartmentName "look right" ?


您好我找到了问题的解决方案



其实在定义中usercontrol我已经给了



Hi I found the solution for the issues

Actually in the definition of usercontrol i have given as

DepartmentID = (TextBox)(Department.FindControl("DepartmentID"));
DepartmentName = (TextBox)(Department.FindControl("DepartmentName"));



这应该是什么错误




Which is wrong it should be

DepartmentID = (TextBox)(Department.FindControl("DepartmentID"));
DepartmentName = (TextBox)(Department.FindControl("Department"));





现在工作正常:)



感谢所有对我的问题感兴趣的人。



Now it works fine :)

Thanks for all who showed interest on my Question.


这篇关于NullReference异常由用户代码取消了解。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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