对象引用未设置为对象的实例 [英] Object reference not set to an instanc of an object

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

问题描述

当我尝试使用VIEW IN BROWSER选项从IDE打开页面时,得到
对象引用未设置为对象的实例" 错误.
我收到此错误的代码如下:

 XResult = Request.QueryString [" ];
TextBox1.Text = XResult.ToString(); 


解决方案

Request对象为null,或者XResult为null,这意味着查询字符串元素"res"不存在于查询字符串中. /blockquote>

只有两种可能性:XResult或TextBox1为null.

以后不太可能-您应该知道是否是这样.

最有可能的是XResult为null,这意味着Request.QueryString返回null.造成这种情况的最可能原因很简单:您是否检查过表单上是否有名称为"res"的输入?


假设,

如果Request.QueryString ["res"]为Null,第二行会发生什么情况.

您正在尝试Conversion of null to string.这就是出现''Object reference not set to an instance of an object''错误的原因.
所以,你要做的是,

检查查询字符串是否不为null,然后进行转换.像这样

 如果(Request.QueryString [" ]!= )
{
XResult = Request.QueryString [" ];
TextBox1.Text = XResult.ToString();
}
其他
{
//  ----不带查询字符串的处理

} 


When I try to open the page from IDE using VIEW IN BROWSER option, I get
"Object reference not set to an instance of an object" error.
The code I get this error is as follows:

XResult = Request.QueryString["res"];
TextBox1.Text = XResult.ToString();


Thanks.

解决方案

Either the Request object is null, or XResult is null, meaning the querystring element "res" does not exist in the querystring.


Only two possibilities: either XResult or TextBox1 is null.

The later is unlikely - you should know if it is.

The most likely is that the XResult is null, implying that the Request.QueryString is returning null. The most likely reason for this is simple: Have you checked that there is a input on your form with the name "res"?


Assume,

if Request.QueryString["res"] it is Null, What happens in second line.

Your are trying to Conversion of null to string. That is reason for getting ''Object reference not set to an instance of an object'' error.

So, What you have to do is,

Check if Query string is not null then do conversion. like this

if(Request.QueryString["res"]!=null)
{
XResult = Request.QueryString["res"];
TextBox1.Text = XResult.ToString();
}
else
{
//---- Handle with out query string

}


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

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