好的,我将遵循 Cristian Darie 所著《电子商务中的 ASP.NET 入门》一书中的示例。该示例构建了一个名为 BalloonShop 的在线商店。我一直忙到第 17 章左右,此时我的网站由于此错误而不再启动:
Object reference not set to an instance of an object
Line 27: HttpContext context = HttpContext.Current;
Line 28: // try to retrieve the cart ID from the user cookie
Line 29: string cartId = context.Request.Cookies["BalloonShop_CartID"].Value;
Line 30: // if the cart ID isn't in the cookie...
Line 31: {
描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。
我的堆栈跟踪如下:
[NullReferenceException: Object reference not set to an instance of an object.] ShoppingCartAccess.get_shoppingCartId() in f:\TheGate\App_Code\ShoppingCartAccess.cs:29 ShoppingCartAccess.GetItems() in f:\TheGate\App_Code\ShoppingCartAccess.cs:188 UserControls_CartSummary.PopulateControls() in f:\TheGate\UserControls\CartSummary.ascx.cs:25 UserControls_CartSummary.Page_PreRender(Object sender, EventArgs e) in f:\TheGate\UserControls\CartSummary.ascx.cs:18 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, >EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnPreRender(EventArgs e) +8998946 System.Web.UI.Control.PreRenderRecursiveInternal() +103 System.Web.UI.Control.PreRenderRecursiveInternal() +175 System.Web.UI.Control.PreRenderRecursiveInternal() +175 System.Web.UI.Control.PreRenderRecursiveInternal() +175 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496
就我有限的知识而言,我的代码似乎没问题(如下所示,来 self 的 ShoppingCartAccess.cs 类):
private static string shoppingCartId
{
get
{
// get the current HttpContext
HttpContext context = HttpContext.Current;
// try to retrieve the cart ID from the user cookie
string cartId = context.Request.Cookies["BalloonShop_CartID"].Value;
// if the cart ID isn't in the cookie...
{
// check if the cart ID exists as a cookie
if (context.Request.Cookies["BalloonShop_CartID"] != null)
{
// return the id
return cartId;
}
else
// if the cart ID doesn't exist in the cookie as well, generate a new ID
{
// generate a new GUID
cartId = Guid.NewGuid().ToString();
// create the cookie object and set its value
HttpCookie cookie = new HttpCookie("BalloonShop_CartID", cartId);
// set the cookie's expiration date
int howManyDays = TheGateConfiguration.CartPersistDays;
DateTime currentDate = DateTime.Now;
TimeSpan timeSpan = new TimeSpan(howManyDays, 0, 0, 0);
DateTime expirationDate = currentDate.Add(timeSpan);
cookie.Expires = expirationDate;
// set the cookie on the client's browser
context.Response.Cookies.Add(cookie);
// return the CartID
return cartId.ToString();
}
}
}
}
我在编程学习的这个阶段非常无助。据我所知,我的程序正在寻找一个 cookie,如果它没有看到它,它就会创建一个,但不知何故,现在它都出错了。在第 16 章之前我都做得很好,但现在我有点搞砸了,因为我不知道如何解决它。有任何想法吗?谢谢!!
最佳答案
由于第 29 行是这样的:
string cartId = context.Request.Cookies["BalloonShop_CartID"].Value;
根据错误,唯一可能的情况是:
- HTTP 上下文不可用(因为您位于用户控件中,所以这种情况不太可能发生)。
- HTTP 请求或 cookies 集合为空(不可能以获取 HTTP 上下文的方式)
Cookies["BalloonShop_CartID"]
返回 null,当您访问.Value
(最有可能)时,此错误会爆发
这些是对象引用异常的唯一可能原因,并且很可能是最后一项,cookie 返回 null。查看整个代码示例,奇怪的是它检查 cookie,然后进行空检查以查看 cookie 是否不为空;它应该执行以下操作(删除第 29 行)。
HttpContext context = HttpContext.Current;
// check if the cart ID exists as a cookie
if (context.Request.Cookies["BalloonShop_CartID"] != null)
{
// return the id
return context.Request.Cookies["BalloonShop_CartID"].Value;
}
else
// if the cart ID doesn't exist in the cookie as well, generate a new ID
{
.
.
关于asp.net - ASP Cookie "Object reference not set to an instance of an object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15814925/