你调用的对象是空的
当我尝试使用“在浏览器中查看”选项从 VS 2008 中的 IDE 打开页面时,出现“对象引用未设置为对象实例”错误。
我收到此错误的代码段:
XResult = Request.QueryString["res"];
TextBox1.Text = XResult.ToString();
When I try to open the page from my IDE in VS 2008 using "VIEW IN BROWSER" option I get "Object reference not set to an instance of an object" error.
The piece of code I get this error :
XResult = Request.QueryString["res"];
TextBox1.Text = XResult.ToString();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这里的问题是
XResult
为null
,当您对其调用ToString
时,代码会产生NullReferenceException
。您需要通过执行显式null
检查来解决此问题The problem here is that
XResult
isnull
and when you callToString
on it the code produces aNullReferenceException
. You need to account for this by doing an explicitnull
check如果您打开的页面没有“res”查询字符串,那么您需要在对其执行任何操作之前检查是否为空。
If you are opening the page without the "res" query string then you need to include a check for null before you do anything with it.
该错误可能是因为 REquest.QueryString 方法在 url 中找不到“res”的值,因此当您尝试对空对象执行“toString”时会引发该异常。
That error could be Because the REquest.QueryString method did not find a value for "res" in the url so when you try to do the "toString" to a null object whrow that exeption.
您的代码需要一个查询字符串
http://localhost:xxxx/yourapp?res=yourval
。它不存在于提供给浏览器的地址中。在项目属性的 web 部分中,您可以提供适当的 URL。当然,建议加强您的代码以实现这一点。Your code is expecting a query string
http://localhost:xxxx/yourapp?res=yourval
. It's not present in the address supplied to the browser. In the web section of your project properties, you can supply an appropriate URL. Of course, shoring up your code to allow for this would be advisable.XResult 已经是一个字符串,因此不需要调用 ToString。这也应该可以解决你的问题。
XResult is already a string, so calling ToString isn't necessary. That should also fix your problem.
这里的问题是 XResult 为 null,当您调用
ToString
其上的代码会产生一个
NullReferenceException
。您需要通过执行显式空检查来解决此问题:The problem here is that XResult is null, and when you call
ToString
on it the code produces a
NullReferenceException
. You need to account for this by doing an explicit null check: