-1

All of a sudden I'm getting an error: Object reference not set to an instance of an object. I'm using Entity Framework 6.0 and everything was working and all of a sudden everything stopped code:

List<Price> prices = db.Prices.ToList() ?? new List<Price>();
if ( prices != null && prices.Any() )
{
      // Price is a data model generated by edmx
      Price price = prices.FirstOrDefault();
}

Anyone knows what could've changed? I've seen other threads have Glimpse in their stack trace but mine doesn't have it, Stack Trace:

at ASP._Page_Views_FAStock__VehiclePrices_cshtml.Execute() in c:\wamp\www\netauto.co.za\apcloud.co.za\ApCloud\ApCloud.SM.UI\Views\FAStock\_VehiclePrices.cshtml:line 96
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
Morgs
  • 1,558
  • 3
  • 19
  • 34
  • `prices` shouldn't be `null`. Have you checked whether you have a problem with `db`. Might it be that this object is null? – Michael Mairegger May 07 '15 at 13:08
  • 1
    Code never stops working "all of a sudden". _Something_ changed, and we can't figure that out from what you've shown. Go debugging, see [duplicate](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) for hints on how to do that. – CodeCaster May 07 '15 at 13:15
  • @xxMUROxx, I've simplified the question for more clarity, what's breaking is as simple as calling the FirstOrDefault() function on the list. – Morgs May 07 '15 at 13:21
  • @CodeCaster, I know what a NullReferenceExcemption and if you don't understand the question better not mark this answer as duplicate! Perhaps read the question again like I said, I've seen that other people are having a similar problem with EF 6+! – Morgs May 07 '15 at 13:22
  • Again, there's not enough information in your question to properly answer it. It's not like _"Oh yeah Entity Framework 6 randomly throws NullReferenceException if you use Glimpse in the same project"_, if that's what you're looking for. At least show the full stack trace and point out the specific method call or property access where this exception is thrown. – CodeCaster May 07 '15 at 13:23
  • @CodeCaster, I've added the StackTrace. I still don't understand why this was marked as a duplicate question? – Morgs May 08 '15 at 07:41
  • Like I said, the `NullReferenceException` is one you can easily debug yourself. How to do that is explained in the duplicate. As for your stack trace, that shows the code you show is irrelevant: the exception occurs in the view `_VehiclePrices.cshtml`, on line 96. Go debugging. If you have enough information, you can edit that into your question. A valid question where SO could help, would be _"Why is variable X on line 96 `null`?"_, but only if you show all relevant code and data. – CodeCaster May 08 '15 at 07:48

1 Answers1

0

Are you sure the error goes on the line with FirstOrDefault()?

List<Price> prices = db.Prices.ToList() ?? new List<Price>();

Here, an exception will be thrown if db, or db.Prices, is null. Using a ?? operator is useless here, because ToList() always returns a list instance.

Price price = prices.FirstOrDefault() ?? new Price() { Amount = 0 };

Could Price constructor be throwing an exception?

André Werlang
  • 5,839
  • 1
  • 35
  • 49