mustafa00
mustafa00

Reputation: 891

System.NullReferenceException shows up after updating NuGet packages

I created a new MVC project (simple template), run it and everyting was ok. But then I updated NuGet packages and when I run my project then the error showed up:

System.NullReferenceException: Object reference not set to an instance of the object

In the debug mode this error comes from this line:

@Scripts.Render("~/bundles/bootstrap")

I didn't touch anything in my code, just updated NuGet packages. My bundle looks standard, like this:

        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*"));

            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"));
        }

I have Bootstrap installed and all files are in proper directories (css in Content and js in Scripts). Interesting thing is that when I changed ScriptBundle to Bundle then it started working. Anyways I don't think it's a proper solution, do you have any ideas how to make it work with standard ScriptBundle?

Upvotes: 3

Views: 2509

Answers (1)

Safyan Yaqoob
Safyan Yaqoob

Reputation: 552

Try to replace:

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

With:

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

In bundle config.

This new Bundle() will bundle your assets without minification. Some cases where minifying just wouldn't work with certain libraries, so this will still allow you to include them in your bundle.

The only difference between a ScriptBundle and a Bundle is that the ScriptBundle uses a JsMinify transform (a default one) – this is the same for the StyleBundle vs the Bundle as well (uses a CssMinify). This minification on the script bundle will ALWAYS happen, even in debug mode.

That’s because commonly in MVC you’ll use the @Scripts.Render HTML Helper to render your bundles. This helper will enumerate the bundle and add the direct paths to the files avoiding the bundling route altogether for debug mode. However, if you truly want to show a bundle that is not minified from the bundle route, you’ll need to use “Bundle” and not “ScriptBundle” (or clear the transformations from the ScriptBundle / StyleBundle)

Upvotes: 7

Related Questions