2012-03-06 65 views
0

我在我的智慧結束了。我一直在下面的代碼行獲得一個NullReferenceException:NullReferenceException *在ViewBag中設置*值

ViewBag.PaypalError = "we were unable to retreive your cart."; 

我知道這是該行 - 我造成它得到一個新的行號文件中的其他地方添加一些代碼,並在我的錯誤的行號改爲遵循它。我知道ViewBag不爲null,因爲我在它之前特別插入了一個if (ViewBag == null)測試。

爲了使事情變得更爲重要,我有代碼在執行進入導致上述語句的邏輯時發送電子郵件。該電子郵件永遠不會被設置,但這行代碼後來發生,會引發一個異常,並且我從try/catch塊中得到了捕獲它的電子郵件。

我知道我應該避開ViewBag,我有一些重構代碼的想法,在這裏不需要它,但沒有一個告訴我這個異常來自哪裏。

任何想法?測試嘗試?


編輯: 下面是其餘的代碼。這絕對不是從catch塊的代碼,我很自豪,但它應該工作...

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"); 
      } 
     } 
    } 

電子郵件:

System.NullReferenceException:對象未設置爲一個對象的實例。 C:############# \ Website \ Controllers \ CartController.cs:Website 137

Timestamp:3/6/2012 10:43:13 AM
IP:#############
URL請求:/ Cart/PaypalConfirmation?token = EC-10L01937X56050826 & PayerID = ###### ######

用戶代理:Mozilla的/ 5.0(Macintosh上,英特爾的Mac OS X 10.6; RV:10.0.2)的Gecko/20100101 Firefox的/ 10.0.2

+3

你可以在該行前後添加幾行以供參考? – 2012-03-06 20:21:39

+0

你可以在try catch塊中顯示你的代碼嗎? – 2012-03-06 21:03:09

+0

@JoachimIsaksson,謝謝你的提問。我打算包括它,但忘記了。這裏是。 – Bobson 2012-03-06 21:31:08

回答

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"); 
    } 
} 
+0

我已經嘗試過了 - 應該會有一封電子郵件,在它到達該行之前我沒有收到。有關於該行的評論。儘管如此,我還沒有嘗試將它作爲內置/替換字符串來使用。我會切換到該方法,看看它是否有幫助。 – Bobson 2012-03-08 14:56:03

+0

我終於有機會實現這種類型的日誌記錄了,而且我從未看到過這個錯誤。這是否意味着我意外地改變了將其作爲其一部分的東西,或者這意味着我們對Paypal的其他更改中的一項消除了我不知道的問題。無論哪種方式,這是一個很好的答案。 – Bobson 2012-04-20 15:15:03

+0

爲了跟進,使用這種類型的日誌記錄(加上大量的手動空檢查),我設法隔離了這個問題。原來,在錯誤中報告的行號始終是有問題的實際行中的5行。再次感謝重新引導我的想法! – Bobson 2012-05-04 13:15:30

相關問題