我在这里结束了我的智慧。我在以下代码行中不断收到NullReferenceException:
ViewBag.PaypalError = "we were unable to retreive your cart.";
我知道就是那条线 - 我在文件的其他地方添加了一些代码,导致它获得一个新的行号,并且我的错误中的行号改为跟随它。我知道ViewBag不是null,因为我在它之前专门插入了if (ViewBag == null)
测试。
为了使事情变得更奇怪,我有代码在执行进入导致上述声明的逻辑时发送电子邮件。该电子邮件永远不会被设置,但是之后发生的这行代码会引发异常,并且我从try / catch块中获取了捕获它的电子邮件。
我知道我应该避免使用ViewBag,并且我有一些想法可以重构代码而不需要它,但是没有一个能告诉我这个异常的来源。
有什么想法吗?试试试试?
编辑: 这是代码的其余部分。这绝对不是我为之骄傲的代码,但它应该有效......
public ActionResult PaypalConfirmation(string token, string payerID)
{
try
{
Cart cart = GetCurrentCart();
if (cart == null)
{
// I never get this email.
SendEmail("Paypal confirmation with null cart", "Token: " + token + ".<br><br>", requestData: Request);
if (token != null && token != "")
{
var tempCart = Cart.GetByAlternateOrderNumber(token);
if (tempCart != null)
{
cart = tempCart;
}
else
{
ViewBag.PaypalError = "we were unable to retreive your cart.";
return View("~/Views/Error/PayPal.cshtml");
}
}
else
{
if (ViewBag == null)
{
SendEmail("VIEWBAG WAS NULL", "Token: " + token + ".<br><br>", requestData: Request);
return View("~/Views/Error/PayPal.cshtml");
}
else
{
// Line which errors
ViewBag.PaypalError = "we were unable to retreive your cart.";
return View("~/Views/Error/PayPal.cshtml");
}
}
}
// More execution code here, including the "Everything worked" return.
}
catch (Exception ex)
{
try
{
var isNull = "";
if (ViewBag == null) isNull = "ViewBag was null!<br><br>";
SendEmail("Crash in Paypal Payment", isNull + ex.ToString(), requestData: Request);
return View("~/Views/Error/PayPal.cshtml");
}
catch (Exception ex2)
{
SendEmail("Crash in reporting Paypal Crash!", ex.ToString() + "<br><br>---------------------<br><br>" + ex2.ToString());
return View("~/Views/Error/PayPal.cshtml");
}
}
}
来自catch区块的电子邮件:
System.NullReferenceException:未将对象引用设置为对象的实例。在C:############# \ Website \ Controllers \ CartController.cs:第137行的Website.Controllers.CartController.PaypalConfirmation(String token,String payerID)中
时间戳:3/6/2012 10:43:13上午 IP:#############
请求的URL:/ Cart / PaypalConfirmation?token = EC-10L01937X56050826&amp; PayerID = ############用户代理:Mozilla / 5.0(Macintosh; Intel Mac OS X 10.6; rv:10.0.2)Gecko / 20100101 Firefox / 10.0.2
答案 0 :(得分:1)
我怀疑这是来自ViewBag为空。添加或删除行时,它会更改其下方所有内容的行号。所以它可能是你改变之下的任何代码。如果新代码要求您向类添加新的using
语句,则它可能已更改输出字节码中的每个行号。
您是否尝试过使用电子邮件作为日志记录的形式,而不仅仅是异常捕获?例如,当你尝试这个时会发生什么?不确定ex.Source
属性是否有帮助?
public ActionResult PaypalConfirmation(string token, string payerID)
{
var message = "Trying to confirm paypal.";
SendEmail(message, message);
try
{
message = "Getting current cart.";
SendEmail(message, message);
Cart cart = GetCurrentCart();
message = cart == null
? "Current cart is null."
: "Current cart is not null.";
SendEmail(message, message);
if (cart == null)
{
if (token != null && token != "")
{
message = "Getting temp cart by alternate order number.";
SendEmail(message, message);
var tempCart = Cart.GetByAlternateOrderNumber(token);
message = "Getting temp cart by alternate order number.";
SendEmail(message, message);
if (tempCart != null)
{
message = "Temp cart is not null";
SendEmail(message, message);
cart = tempCart;
}
else
{
message = "Temp cart was null.";
SendEmail(message, message);
ViewBag.PaypalError = "we were unable to retreive your cart.";
message = "ViewBag.PayPalError set, returning view.";
SendEmail(message, message);
return View("~/Views/Error/PayPal.cshtml");
}
}
else
{
message = "Setting ViewBag.PayPalError message.";
SendEmail(message, message);
// Line which errors
ViewBag.PaypalError = "we were unable to retreive your cart.";
message = "ViewBag.PayPalError set, returning view.";
SendEmail(message, message);
return View("~/Views/Error/PayPal.cshtml");
}
}
// More execution code here, including the "Everything worked" return.
message = "Executing more code.";
SendEmail(message, message);
}
catch (Exception ex)
{
SendEmail(ex.GetType().Name + " caught", ex.Source);
return View("~/Views/Error/PayPal.cshtml");
}
}