'MVC 5 @Scripts.Render("~/bundles/bootstrap") error 'Object reference not set to an instance of an object.'

_Layout.cshtml file fails when attempting to render the bootstrap 5 script bundle (works for earlier versions of bootstrap)

Bundle.Config File

_Layout Debugging

Scripts Folder



Solution 1:[1]

Try changing "new ScriptBundle" to "new Bundle" in your "RegisterBundles" within BundleConfig:

bundles.Add(new Bundle("~/bundles/bootstrap").Include("~/Scripts/bootstrap.js"));

Solution 2:[2]

I have the same issue after updating bootstrap to the latest (v5.0.2) via Nuget. I see an answer above about changing ScriptBundle to Bundle with minified js. It is not a real solution for this issue. Script bundling is used to minify scripts only on release mode. We need unminified version on debug mode for debugging. So if you are already using minified js, you do not need to add it to bundleconfig.

I tried bundling with both bootstrap.js and bootstrap.bundle.js and got the same exception at script render phase.

I trimmed very long stacktrace as below:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=WebGrease
  StackTrace:
   at Microsoft.Ajax.Utilities.JSParser.ParseObjectLiteralProperty(Boolean isBindingPattern)
   at Microsoft.Ajax.Utilities.JSParser.ParseObjectLiteral(Boolean isBindingPattern)
   at Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus)
   at Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus)
   at Microsoft.Ajax.Utilities.JSParser.ParseExpressionList(JSToken terminator)
   at Microsoft.Ajax.Utilities.JSParser.ParseMemberExpression(AstNode expression, List`1 newContexts)
   at Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus)
   at Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus)
   at Microsoft.Ajax.Utilities.JSParser.ParseExpressionStatement(Boolean fSourceElement)
   at Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean fSourceElement, Boolean skipImportantComment)
   at Microsoft.Ajax.Utilities.JSParser.ParseBlock()
   at Microsoft.Ajax.Utilities.JSParser.ParseArrowFunction(AstNode parameters)
   at Microsoft.Ajax.Utilities.JSParser.ParseLeftHandSideExpression(Boolean isMinus)
   at Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus)

I think there is a js format issue in new bootstrap.js or bootstrap.bundle.js which causes the parser crash.

Solution 3:[3]

As @Manoj Salvi commented, Bootstrap 5 can be successfully bundled with:

bundles.Add(new Bundle("~/scripts/core").Include("~/content/scripts/bootstrap.bundle.min.js"));

Solution 4:[4]

public class BundleConfig { // Paketleme hakk?nda daha fazla bilgi için lütfen https://go.microsoft.com/fwlink/?LinkId=301862 adresini ziyaret edin. public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/bundles/jquery").Include( "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        // Geli?tirme yapmak ve ö?renme kayna?? olarak yararlanmak için Modernizr uygulamas?n?n geli?tirme sürümünü kullan?n. Ard?ndan
        // üretim için haz?r. https://modernizr.com adresinde derleme arac?n? kullanarak yaln?zca ihtiyac?n?z olan testleri seçin.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js"));

        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/site.css"));

        bundles.Add(new Bundle("~/bundles/bootstrap").Include(
                   "~/Scripts/bootstrap.js",
                   "~/Scripts/respond.js",
                   "~/Content/site.css"));
    }
}

Solution 5:[5]

create new mvc project go to file manager copy all Script and bootstrap files paste your running project.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Manoj Salvi
Solution 2 burcinsahin
Solution 3 quantum
Solution 4 user17502107
Solution 5 puneet