Note that there are some explanatory texts on larger screens.

plurals
  1. PONullReferenceException on if statement with only a bool check
    text
    copied!<p>I'm experiencing a "strange" situation with a live ASP.NET MVC 3 app. The error start on production after X hours of use. I did not have been able to reproduce the error on my development machine.</p> <p>Here is my controller action signature:</p> <pre><code>[HttpPost] public ActionResult AddBanking(int id, int? page, int bankId, string q, int? searchFlag, string query, string accountNo, string accountManager, bool? personnal, int? branchId, int? selectedId, BankSearchModel data) </code></pre> <p>The basic idea is that the controller is use for two forms on the View, one for searching where searchFlag is a Hidden field and another form for adding, here is a basic code structure.</p> <pre><code>try { if (searchFlag.HasValue) { var vm = svc.FetchBanks(bankId, q); return View(vm); } else { bool valid = true, isIndividu = false; // some code if(!valid) // &lt;- this is where the line 106 is { } } } catch(Exception ex) { ex.Data.Add("context", "AddBanking - THIS IS IT"); Log.Error(ex); return RedirectToAction("Error", new { id = 0 }); } </code></pre> <p>The error occurs after long periods of time the app run flawlessly, all of a sudden, that exception is caught, but when the users submit the first form (for searching -> searchFlag = 1), so in my example it entered the first if statement.</p> <p>This is the error and stack trace</p> <pre><code>context: AddBanking - THIS IS IT Message: Object reference not set to an instance of an object. Stack: at XYZ.Controllers.BanksController.AddBanking(Int32 id, Nullable`1 page, Int32 bankId, String q, Nullable`1 searchFlag, String query, String accountNo, String accountManager, Nullable`1 personnal, Nullable`1 branchId, Nullable`1 selectedId, BankSearchModel data) in E:\Projects\XYZ\ABC\123.Web\Controllers\BanksController.cs:line 106 </code></pre> <ol> <li>How can it be possible to throw exception on a if statement with a non-nullable bool ?</li> <li>Since there is a return View() in my first if block, and I'm 100 % certain it entered there how come the exception is on the line 106 where the execution should not even go there.</li> <li>This is something that happen only after long period of production time, yesterday (8 to 5) this process worked, and this morning bang the exception is throwing, but only in production (Windows 2008, ASP.NET MVC 3). From my developement machine with the same database and same data posting to the page is working.</li> </ol> <p>My main question, how am I suppose to debug this, at first I thought that the stack trace does not returned the right line number, so I added a try/catch inside the action, yet it still return the if(!valid) as NRE...</p> <p>I would appreciate any idea / guide line. Of course I could separate the two process inside the action and it might fix the problem, but I would rather like to understand what I'm doing wrong in the example.</p> <p><strong>Edit: I'm completely clueless</strong> By the time I asked this question, the page worked without any change in code and for the exact same data posted.</p> <p><strong>Edit 2 @ 10:30 2011/06/01</strong></p> <p>Thanks for your suggestions, but I'm really thinking this is really something harder to find. The bug did re-appear today but the app was working from 1 hour after I post this question yesturday until today @ 10 am.</p> <p>Here is more detail on what I've done so far to try to understand what's going on:</p> <ol> <li>I've rebuilt the app (no code changes, and re-deploy bin, views) with .pdb files.</li> <li>I've put try/catch on every path that the execution should take here is the exact scenario:</li> </ol> <p>View:</p> <pre><code>@using (Html.BeginForm()) { @Html.ValidationSummary() &lt;fieldset&gt; @Html.Hidden("searchFlag", 1) @Html.Textbox("q") &lt;input type="submit" value="Chercher" /&gt;&lt;/td&gt; &lt;/fieldset&gt; } </code></pre> <p><strong>svc cannot be null</strong> because I have it set on the controller like this:</p> <pre><code>[Authorize] public class BanksController : BaseController { BankService svc = new BankService(); ... } </code></pre> <p>The FetchBanks method</p> <pre><code>public BankSearchModel FetchBanks(int bankId, string query) { return Execute&lt;BankSearchModel&gt;(db =&gt; { try { // some linq stuff } catch(Exception ex) { XYZ.Log.Error(ex); // &lt;= note1 }, "Error returned by Execute"); } </code></pre> <p>My Execute method</p> <pre><code>protected static T Execute&lt;T&gt;(Func&lt;XYZDbContext, T&gt; query, string errorMessage, bool deferredLoading) { try { using (XYZDbContext db = new XYZ...()) { #if DEBUG db.Log = new DebuggerWriter(); db.CommandTimeout = 3 * 60; #endif if (!deferredLoading) db.DeferredLoadingEnabled = false; return query(db); } } catch (Exception ex) { #if DEBUG Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace.ToString()); throw; #else ex.Data.Add("context", "Manager, executing query"); XYZ.Log.Error(ex); return default(T); #endif } } </code></pre> <p>So again, I understand that the line 106 is not the real error here, but I have try/cath in every single method that the execution should go when the searchFlag is set to 1 (from the first form of the view. Still the only Log.Error(ex) I got was the one telling me there is an error on my contorller at line 106.</p> <p><strong>Note1</strong> If the error would have come from the FetchBanks method it would have entered the catch inside that method and send me and email with stack trace of that method.</p> <p>At the end of my FetchBanks I return a BankSearchModel which as all List so all databases query are executed before returning to the controller.</p> <p><strong>And again, how can it work for the same data posted all day long and suddently stop working</strong>. Would that be possible that the signature of the controller with all my null object would cause that behaviour?</p> <p>This is the only email I got after adding try/catch everywhere that should have send me an error. i.e no other method are entering their catch (The execute, the FetchBanks) only the controller action. I would really like to understand what's going on.</p> <pre><code>New error occured at 6/1/2011 9:48:13 AM context: AddBanking - THIS IS IT Message: Object reference not set to an instance of an object. Stack: at XYZ.Web.Controllers.BanksController.AddBanking(Int32 id, Nullable`1 page, Int32 </code></pre> <p>bankId, String q, Nullable<code>1 searchFlag, String query, String accountNo, String accountManager, Nullable</code>1 personnal, Nullable<code>1 branchId, Nullable</code>1 selectedId, BankSearchModel data) in E:\Projects\HiddenForObiousReason\Controllers\BanksController.cs:line 106</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload