Object reference not set to an instance of an object in _Layout.cshtml after updated packages

C#asp.net MvcRazorasp.net Mvc-5Owin

C# Problem Overview


The project was running fine until I updated all NuGet packages via NuGet Manager (I was trying to get the latest OWIN packages).

This error is in the _Layout.cshtml and I have not touched it at all.

> Object reference not set to an instance of an object. > > Description: An unhandled exception occurred during the execution of > the current web request. Please review the stack trace for more > information about the error and where it originated in the code. > > Exception Details: System.NullReferenceException: Object reference not > set to an instance of an object. > > Source Error: > > > Line 13:
> Line 14: > Line 15: @Scripts.Render("~/Content/fonts") > > Source File: f:\Workplace\lrc\lrc\Views\Shared_Layout.cshtml Line: > 15


Update

Here is the code in the _Layout.cshtml (they are automatically generated when creating the project):

@ViewBag.Title - LRC @Styles.Render("/Content/css") @Scripts.Render("/bundles/modernizr")

    <!-- Custom Fonts -->
    @Scripts.Render("~/Content/fonts")
    
    <link href="http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="http://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">

So it is complaining this line of code:

@Scripts.Render("~/Content/fonts")

But as you can see that, this is VS generated code, how could I make it wrong?

I also checked the content in the Conntent/font-awesome/fonts folder and all required resources are there.

Here is the code inside the BundleConfig (which is also the auto-generated code):

bundles.Add(new ScriptBundle("/Content/fonts").Include( "/Content/font-awesome/fonts/fontawesome-*", "~/Content/font-awesome/fonts/FontAwesome.otf"));

My Microsoft.OWin.Security, Microsoft.OWin.Security.Google and Microsoft.OWin are version 3.0.1.0.


Update 2

Here is the stack trace:

> [NullReferenceException: Object reference not set to an instance of an > object.] Microsoft.Ajax.Utilities.JSParser.ParseExpression(AstNode > leftHandSide, Boolean single, Boolean bCanAssign, JSToken inToken) > +1007 Microsoft.Ajax.Utilities.JSParser.ParseExpressionStatement(Boolean > fSourceElement) +268
> Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean > fSourceElement, Boolean skipImportantComment) +1552
> Microsoft.Ajax.Utilities.JSParser.ParseBlock() +164
> Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean > fSourceElement, Boolean skipImportantComment) +498
> Microsoft.Ajax.Utilities.JSParser.ParseBlock() +164
> Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean > fSourceElement, Boolean skipImportantComment) +498
> Microsoft.Ajax.Utilities.JSParser.ParseBlock() +164
> Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean > fSourceElement, Boolean skipImportantComment) +498
> Microsoft.Ajax.Utilities.JSParser.ParseStatements(Block block) +159
> Microsoft.Ajax.Utilities.JSParser.InternalParse() +1011
> Microsoft.Ajax.Utilities.JSParser.Parse(DocumentContext sourceContext) > +123 Microsoft.Ajax.Utilities.JSParser.Parse(DocumentContext sourceContext, CodeSettings settings) +54
> Microsoft.Ajax.Utilities.Minifier.MinifyJavaScript(String source, > CodeSettings codeSettings) +987
> System.Web.Optimization.JsMinify.Process(BundleContext context, > BundleResponse response) +310
> System.Web.Optimization.Bundle.ApplyTransforms(BundleContext context, > String bundleContent, IEnumerable1 bundleFiles) +263 > System.Web.Optimization.Bundle.GenerateBundleResponse(BundleContext > context) +355 > System.Web.Optimization.Bundle.GetBundleResponse(BundleContext > context) +104 > System.Web.Optimization.BundleResolver.GetBundleContents(String > virtualPath) +254 > System.Web.Optimization.AssetManager.DeterminePathsToRender(IEnumerable1 > assets) +252
> System.Web.Optimization.AssetManager.RenderExplicit(String tagFormat, > String[] paths) +75
> System.Web.Optimization.Scripts.RenderFormat(String tagFormat, > String[] paths) +292
> System.Web.Optimization.Scripts.Render(String[] paths) +51
> ASP._Page_Views_Shared__Layout_cshtml.Execute() in > f:\Workplace\lrc\lrc\Views\Shared_Layout.cshtml:15
> System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271
> System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +121
> System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext > pageContext, TextWriter writer, WebPageRenderingBase startPage) +145
> System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext > pageContext, TextWriter writer) +41
> System.Web.WebPages.<>c__DisplayClass3.b__2(TextWriter > writer) +335 System.Web.WebPages.HelperResult.WriteTo(TextWriter > writer) +42
> System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, > HelperResult content) +45
> System.Web.WebPages.WebPageBase.Write(HelperResult result) +53
> System.Web.WebPages.WebPageBase.RenderSurrounding(String > partialViewName, Action1 body) +178 > System.Web.WebPages.WebPageBase.PopContext() +229 > System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext > pageContext, TextWriter writer, WebPageRenderingBase startPage) +154 > System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, > TextWriter writer, Object instance) +695 > System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext > viewContext, TextWriter writer) +382 > System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) > +431 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext > controllerContext, ActionResult actionResult) +39 > System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 > filters, Int32 filterIndex, ResultExecutingContext preContext, > ControllerContext controllerContext, ActionResult actionResult) +116
> System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 > filters, Int32 filterIndex, ResultExecutingContext preContext, > ControllerContext controllerContext, ActionResult actionResult) +529 > System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext > controllerContext, IList1 filters, ActionResult actionResult) +106
> System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() > +321 System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult > asyncResult) +185
> System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult > asyncResult) +42 > System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +133
> System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, > Object tag) +56
> System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult > asyncResult) +40
> System.Web.Mvc.Controller.b__1d(IAsyncResult > asyncResult, ExecuteCoreState innerState) +34
> System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult > asyncResult) +70 > System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +139
> System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, > Object tag) +59
> System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, > Object tag) +40
> System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 > System.Web.Mvc.Controller.b__15(IAsyncResult > asyncResult, Controller controller) +39
> System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult > asyncResult) +62 > System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +139
> System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, > Object tag) +59
> System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, > Object tag) +40 System.Web.Mvc.Controller.EndExecute(IAsyncResult > asyncResult) +39
> System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult > asyncResult) +39
> System.Web.Mvc.MvcHandler.b__5(IAsyncResult > asyncResult, ProcessRequestState innerState) +39
> System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult > asyncResult) +70 > System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +139
> System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, > Object tag) +59
> System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, > Object tag) +40
> System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) > +40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult > result) +38
> System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() > +932 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +188


Update 3

Here is the Content folder:

enter image description here


Update 4

Here is the content of the packages.config:

C# Solutions


Solution 1 - C#

Here it was fixed by replacing "ScriptBundle" with "Bundle" in BundleConfig.

We replaced the following:

bundles.Add(new ScriptBundle("~/bundles/mybundle").Include(
            "~/Scripts/...",
            "~/Scripts/..."));

with:

bundles.Add(new Bundle("~/bundles/mybundle").Include(
            "~/Scripts/...",
            "~/Scripts/..."));

Solution 2 - C#

Check javascript for errors. Mine was a half written line ending with 'function'

Solution 3 - C#

This looks like a bug in the javascript minifier.

Microsoft.Ajax.Utilities.Minifier.MinifyJavaScript(String source, CodeSettings codeSettings) +987

Please file a connect bug (i'll forward this thread internally in Microsoft as well).

As a workaround you can minify the scripts manually and include them directly instead of using the bundler.

Can you share your packages.config please? It will help with tracking this potential bug.

Solution 4 - C#

So I ran into this bug earlier today and was able to track down the offending code. For myself, the minifier was choking on a bad line of javascript that was incorrectly using a ternary statement of the form:

var test = condition ? true_value ? false_value;

Obviously, the second question mark should have been a colon to make a valid ternary statement. This caused the entire application to crash throwing the same error above. Perhaps this isn't the same issue you ran into, but it gives the same results.

Solution 5 - C#

ScriptBundleclass can be used for bundling scripts and cannot be used for bundling styles. It is coupled with @Scripts.Render("~/scripts/bundle") that renders a <script> tag.

For styles there is StyleBundle class

bundles.Add(new StyleBundle("~/bundles/mybundle").Include(
        "~/Content/...",
        "~/Content/..."));

and it is coupled with @Styles.Render("~/content/bundle") that creates a <link> tag.

Solution 6 - C#

In my case, it's due to changes in verdor libs so I can't fix the js file directly.

My work around is to use the min version which is indicate to the bundle: DO not minify this file.

For example: Simply move from select2.js to select2.min.js

Solution 7 - C#

There must be a javascript error.

Undo your changes and just check your changes by putting one by one and check the browser with those changes as well.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFranvaView Question on Stackoverflow
Solution 1 - C#Ron DeijkersView Answer on Stackoverflow
Solution 2 - C#Simon LeggView Answer on Stackoverflow
Solution 3 - C#Yishai GalatzerView Answer on Stackoverflow
Solution 4 - C#jamesmillerioView Answer on Stackoverflow
Solution 5 - C#Martin StaufcikView Answer on Stackoverflow
Solution 6 - C#hakuna1811View Answer on Stackoverflow
Solution 7 - C#Mahavirsinh PadhiyarView Answer on Stackoverflow