1

I started getting a System.NullReferenceException in the _Layout.cshtml file of my project. It is happening on line 16 - @Scripts.Render("~/bundles/modernizr"). I removed the bundle and the error just hit a lineup. So I removed all bundles and still, it just moved to the title line above that.

Here are the error details:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=App_Web_erjqyhfy
  StackTrace:
   at ASP._Page_Views_Shared__Layout_cshtml.Execute() in C:\My_Code\CompetitionTracker\CompetitionTracker\Views\Shared\_Layout.cshtml:line 16
   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.WebPages.WebPageBase.<>c__DisplayClass40_0.<RenderPageCore>b__0(TextWriter writer)
   at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
   at System.Web.WebPages.WebPageBase.Write(HelperResult result)
   at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)
   at System.Web.WebPages.WebPageBase.PopContext()
   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.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

Here is my head in _Layout:

<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>@ViewBag.Title | @ConfigurationManager.AppSettings["appName"]</title>

    @Styles.Render("~/Content/css")

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/modernizr")
    <script src="~/Scripts/Site.js"></script>
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    <script src="https://kit.fontawesome.com/92e259da1e.js" crossorigin="anonymous"></script>
</head>

When I remove items that are causing the exceptions, I end up getting this tab that opens... Source Not Available

I am at a loss! Please help! What could possibly be causing this!? I really don't think it has anything to do with the actual line where the error is throwing!

urmat abdykerimov
  • 427
  • 1
  • 7
  • 17
Alyson
  • 11
  • 5
  • Remove parts of the page until the issue goes away. When the issue goes away, the last thing you removed was the cause. – mjwills Jul 13 '21 at 00:41
  • @mjwills I removed all the bundle references, and the title line, now I get a "Source Not Available" tab that opens along with the same NullReferenceException – Alyson Jul 13 '21 at 01:04
  • Remove _everything_. Does it fail? Now add in code, a line at a time. Which line causes the issue? My bet is on `@ViewBag.Title | @ConfigurationManager.AppSettings["appName"]`. Or whatever line 16 is. – mjwills Jul 13 '21 at 01:04
  • I still get the NullReferenceException when that Source Not Available page comes up. So I'm still getting the error. When I add the title back in, it goes back to throwing the exception on that line. – Alyson Jul 13 '21 at 01:08
  • @JohnG, no that doesn't address my issue. It is 99% for VB not MVC, and also doesn't address the exception on the Layout page or for the Bundles or ConfigurationManager references – Alyson Jul 13 '21 at 01:12
  • Have you stepped through the code line by line in the debugger and examined each property? Something is `null` and it is irrelevant what code or framework you are using. `null `is `null` and it is your job to figure out “what” is `null`. Again, have you stepped through the code and examined each variable/property to see “”what” variable/property is `null`. In most cases once you know “which” variable is `null`, it is simply a matter of back tracking to see “why” it is `null`. My question would be “what” line of code throws this error? – JohnG Jul 13 '21 at 01:25
  • Yes I stepped through every step and everything is fine until it throws the error on the @Scripts.Render("~/bundles/modernizr") line – Alyson Jul 13 '21 at 01:41
  • OK… the `null` error is thrown on the line… `@Scripts.Render("~/bundles/modernizr")` … THEN, what part of that code is `null`? – JohnG Jul 13 '21 at 01:52
  • @JohnG - I have no idea how that can be null... That code has been there since the beginning and just started throwing the error today... That's why I need help, the bundles haven't been changed since it was working yesterday. – Alyson Jul 13 '21 at 02:01
  • https://stackoverflow.com/questions/30219807/object-reference-not-set-to-an-instance-of-an-object-in-layout-cshtml-after-upd – mjwills Jul 13 '21 at 04:21

1 Answers1

0

First you added @using System.Configuration namespace to _Layout?

Second in webconfig exists appName?

Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17