[英]Error: Object reference not set to an instance of an object?
For example, in Login.aspx
, I have two link. 例如,在
Login.aspx
,我有两个链接。
<a href="Main.aspx?site=facebook">Facebook_link</a>
<a href="Main.aspx">Google_link</a> // (I wouldn't like to wirte "Main.aspx?site=google"
On Main
Page_Load
, there is : 在
Main
Page_Load
,有:
if(Request.QueryString["site"].ToString()!="facebook")
{
.....
}
On Login page
if I click
Facebook_link
and then I go to Main page
, everything works great. 在
Login page
如果我click
Facebook_link
,然后转到Main page
,则一切正常。
But on Login page
if I click
Google_link
and then I go to Main page
, I get error
. 但是在
Login page
如果我click
Google_link
,然后转到Main page
,则会error
。 [Error: Object reference not set to an instance of an object]
I know why I get this error. 我知道为什么会收到此错误。
I woud like to ask that it is possible to check on Main page
that "If Request.QueryString["site"]
is exist, do this". 我想问问是否可以在
Main page
检查“如果存在Request.QueryString["site"]
,请执行此操作”。
Request.QueryString["site"]
is probably evaluating to null. Request.QueryString["site"]
可能评估为null。
To prevent this happening, you need to check for null like this: 为了防止这种情况的发生,您需要像这样检查null:
var queryString = Request.QueryString["site"];
if (queryString != null && queryString.ToString() != "facebook")
{ .....
}
Try this: 尝试这个:
if (Request.QueryString["site"]!= null && Request.QueryString["site"]!= "facebook")
{ .....
}
QueryString is like session, hidden fields. QueryString类似于会话,隐藏字段。 Once you set a value it will hold it untill you change it.
一旦设置了一个值,它将一直保持到您更改它为止。 you have told that you click facebook link.
您告诉过您单击Facebook链接。 so querystring will hold the value "facebook" even you click on google link.
因此,即使您单击Google链接,querystring也会保留值“ facebook”。 because you haven't set any values on google.
因为您尚未在Google上设置任何值。 So just do like this,
所以就这样吧
<a href="Main.aspx?site=google">Google_link</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.