对象引用未设置为对象的实例......如何解决此错误 [英] Object reference not set to an instance of an object......How to solve this error

查看:97
本文介绍了对象引用未设置为对象的实例......如何解决此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此行从数据库中获取DropDownList中绑定位置的错误(以下方法的第二行是错误行)

Im getting the Error on Binding location in DropDownList from Database on this line( 2nd line in below method that is Error line)

void fun()
{
ddlcompanyname.SelectedItem.Text = Dt.Rows[0]["Company_name"].ToString();---->No Error
ddllocation.SelectedItem.Text = Dt.Rows[0]["Location"].ToString();----->Error
}


""对象引用未设置为对象的实例."""


""""Object reference not set to an instance of an object.""""""

推荐答案

我很高兴您提到了带有和不带有两种情况错误.

如果出现错误,最可能的情况是 Dt.Rows [0] ["Location"].返回为NULL.

在尝试访问方法或属性的NULL对象上,会出现此错误.

希望这会有所帮助!
I am glad that you mentioned the two scenarios with and without error.

In the case where you get an error, the most probable situation would be that Dt.Rows[0]["Location"]. is returned as NULL.

And on a NULL object when you try to access a method or property, you get this error.

Hope this helps!


或者,

您的数据表Dt没有名为位置"的字段/列(检查拼写和大小写)

或者,

ddllocation没有选定的项目

调试以确定是否是两个可能的问题.

如果是两者中的后者,那么您始终可以以编程方式选择一个项目
Either,

Your datatable Dt does not have a field/column called "Location" (check spelling and case)

or,

ddllocation does not have a selected item

Debug to determine which of the two possible problems is the case.

If it is the latter of the two then you can always programmatically select an item


还可以查看解决方案#2到
Also have a look at solution #2 to this question[^] posted just minutes before your own.


这篇关于对象引用未设置为对象的实例......如何解决此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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