【发布时间】:2014-06-21 02:37:47
【问题描述】:
我在 if 条件下得到 nullpointerexception。如何解决此错误。我的代码和错误描述如下。
错误:System.Web.dll 中出现“System.NullReferenceException”类型的异常,但未在用户代码中处理
此行出现错误:if (Request.Cookies[name].Value != null)
public void SetCookie(string name, string value, int expiration)
{
HttpCookie cookie;
cookie = new HttpCookie(name);
if (Request.Cookies[name].Value != null)
{
DeleteCookie(name);
}
else
{
cookie.Value = value;
cookie.Expires = DateTime.Now.AddDays(expiration);
Response.Cookies.Add(cookie);
}
}
【问题讨论】:
-
你知道
NullReferenceException的真正含义吗? -
我认为当浏览器中不存在cookies时会出现这个错误。
-
是的,在这种情况下,几乎可以肯定是这样。但是那个异常是什么意思呢?你的代码说明了什么?