Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC error in BundleConfig class: An exception of type 'System.NullReferenceException' occurred in mscorlib.dll

My MVC website has an Runtime Error on starting in BundleConfig class with the following title:

An exception of type 'System.NullReferenceException' occurred in mscorlib.dll but was not handled in user code

enter image description here

Usings:

using System.Web.Optimization;
using BundleTransformer.Core.Bundles;
using BundleTransformer.Core.Orderers;
using BundleTransformer.Core.Transformers;

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   BundleTransformer.Core.BundleTransformerContext..ctor() +162
   BundleTransformer.Core.BundleTransformerContext.<.cctor>b__0() +44
   System.Lazy`1.CreateValue() +416
   System.Lazy`1.LazyInitValue() +152
   System.Lazy`1.get_Value() +75
   BundleTransformer.Core.BundleTransformerContext.get_Current() +60
   BundleTransformer.Core.Transformers.CssTransformer..ctor(IMinifier minifier, IList`1 translators, IList`1 postProcessors, String[] ignorePatterns) +79
   BundleTransformer.Core.Transformers.CssTransformer..ctor() +97
   NWebsite.BundleConfig.RegisterBundles(BundleCollection bundles) in d:\Documents\Visual Studio 2013\Projects\N\Web\N.Website\App_Start\BundleConfig.cs:18
   NWebsite.MvcApplication.Application_Start() in d:\Documents\Visual Studio 2013\Projects\N\Web\N.Website\Global.asax.cs:20

[HttpException (0x80004005): Object reference not set to an instance of an object.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9916613
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Object reference not set to an instance of an object.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9930508
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

And MvcApplication class in Global.asax file:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

What is it's reasons and how can i solve it?

like image 436
Ramin Bateni Avatar asked Dec 01 '14 15:12

Ramin Bateni


1 Answers

I managed to resolve this. It appears to occur when you are missing the right configuration in your web.config (in my case this occurred due to a missing web config transform for the given environment). So make sure the following is present in your web.config:

<bundleTransformer xmlns="http://tempuri.org/BundleTransformer.Configuration.xsd">
<core>
  <css>
    <translators>
      <add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" />
      <add name="LessTranslator" type="BundleTransformer.Less.Translators.LessTranslator, BundleTransformer.Less" />
    </translators>
    <postProcessors>
      <add name="UrlRewritingCssPostProcessor" type="BundleTransformer.Core.PostProcessors.UrlRewritingCssPostProcessor, BundleTransformer.Core" useInDebugMode="false" />
    </postProcessors>
    <minifiers>
      <add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" />
    </minifiers>
    <fileExtensions>
      <add fileExtension=".css" assetTypeCode="Css" />
      <add fileExtension=".less" assetTypeCode="Less" />
    </fileExtensions>
  </css>
  <js>
    <translators>
      <add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" />
    </translators>
    <minifiers>
      <add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" />
    </minifiers>
    <fileExtensions>
      <add fileExtension=".js" assetTypeCode="JavaScript" />
    </fileExtensions>
  </js>
</core>
<less>
  <jsEngine name="MsieJsEngine" />
</less>
</bundleTransformer>
<jsEngineSwitcher xmlns="http://tempuri.org/JavaScriptEngineSwitcher.Configuration.xsd">
<core>
  <engines>
    <add name="MsieJsEngine" type="JavaScriptEngineSwitcher.Msie.MsieJsEngine, JavaScriptEngineSwitcher.Msie" />
  </engines>
</core>
</jsEngineSwitcher>
like image 102
IvanL Avatar answered Nov 15 '22 00:11

IvanL