时不时出现-object reference not set to an instance of an boject,
客户反映出现 object reference not set to an instance of an boject
我怀疑是的问题 session
我们网站有不少客户,这个问题偶尔会出现,大概1000个人有一个人会有这个问题吧,
是什么问题啊,
下面是cookiele和Session 的取值
public static int EnterpriseID()
{
HttpContext context = HttpContext.Current ;
HttpCookie cookie=context.Request.Cookies["CookieLe"];
if(cookie.Values["enterpriseID"]==null||cookie.Values["enterpriseID"]=="")
{
//return 0;
}
else
{
return int.Parse(jiemi_Des(cookie.Values["enterpriseID"]));
}
if (context.Session["enterpriseID"] == null || context.Session["enterpriseID"].ToString() == "")
{
//return 0;
}
else
{
return int.Parse(context.Session["enterpriseID"].ToString());
}
return 0;
}
就是这里偶尔会出错的。
public static bool EnterpriseAuthenticated()
{
HttpContext context = HttpContext.Current ;
if (context.Session["enterpriseID"] == null || context.Session["enterpriseID"].ToString() == "")
{
//return(false);
}
else
{
return (true);
}
if (context.Request.Cookies["CookieLe"] == null)
return false;
HttpCookie cookie=context.Request.Cookies["CookieLe"];
//string name=cookie.Values["name"];
//string age=cookie.Values["age"];
if(cookie.Values["enterpriseID"]==null||cookie.Values["enterpriseID"]=="")
{
}
else
{
int intmy = IsNumeric(cookie.Values["enterpriseID"]);
if (intmy > 0)
return false;
else
return true;
}
return false;
}
取值之前我都会判断的,也不知道为啥会出现那个错误。
[解决办法]
帮忙顶一下,有可能是补丁有问题啊
[解决办法]
哪一行出错?
如果连哪一行都不能指出来,你还要怎样来除错呢?所以最关键地是报告尽可能的详尽的异常信息,而不是像你这样(也许是根本没有在意过)把重要的错误行号等信息给剪切掉。
“取值之前我都会判断的”这通常并不能除错。就好像一个得了肺癌的人遇到问题就停止呼吸,这只能让问题爆发在后头,而不能除错。真正的除错态度就是尽早地重现bug,而且认真记录到底是哪一行、哪一个变量是null,并找到其调用堆栈来分析为什么是null。
一旦除错的步骤做过几次,你就根本不需要写多余的“我都会判断null”这种东西了。反之,这就说明你根本没有经过高强度的测试就拿上去给用户凑合用了。
[解决办法]
你并没有确认过 session 是否有效,我指的是 context.Session != null,在 ASP.NET 中,Session 常常会因为一些莫名其妙的原因丢失。
[解决办法]
这样判断是有问题的,如果cookie.Values["enterpriseID"]==null,context.Session["enterpriseID"].ToString()就会出错
[解决办法]
if (context.Session["enterpriseID"] == null
[解决办法]
context.Session["enterpriseID"].ToString() ==
不要直接.ToString()写个方法判断是不是为null
或者写个三元判断一下
if (context.Session["enterpriseID"] == null
[解决办法]
(context.Session["enterpriseID"]==null?true:context.Session["enterpriseID"].ToString()==""))
[解决办法]
if(cookie.Values["enterpriseID"]==null
[解决办法]
cookie.Values["enterpriseID"]=="")
{
}
else
{
int intmy = IsNumeric(cookie.Values["enterpriseID"]);
if (intmy > 0)
return false;
else
return true;
}
应该这么写
if(cookie.Values["enterpriseID"]!=null){
if(cookie.Values["enterpriseID"]!=""){
int intmy = IsNumeric(cookie.Values["enterpriseID"]);
if (intmy > 0)
return false;
else
return true;
}
}