title: Server Error "Object reference not set to an instance of an object" after insert from branch
body: <p>Everything works correctly but after Sitecore update from 7.0 to 7.2 I see the following Server Error when creating a site from a branch     </p>
  
  <pre><code>[NullReferenceException: Object reference not set to an instance of an object.]
     Sitecore.Nexus.Data.DataCommands.AddFromTemplateCommand.(Item , Item , String , ID , ID , String , SafeDictionary`2 ) +420
     Sitecore.Nexus.Data.DataCommands.AddFromTemplateCommand.(Item , Item , String , ID , ID , String , SafeDictionary`2 ) +856
     Sitecore.Nexus.Data.DataCommands.AddFromTemplateCommand.(String , Item , Item , ID ) +569
     Sitecore.Data.Engines.DataCommands.AddFromTemplateCommand.DoExecute() +113
     Sitecore.Data.Engines.EngineCommand`2.Execute() +121
     Sitecore.Data.Engines.DataEngine.AddFromTemplate(String itemName, ID templateId, Item destination, ID newId) +101
     Sitecore.Data.Managers.ItemProvider.AddFromTemplate(String itemName, ID templateId, Item destination, ID newId) +363
     Sitecore.Data.Managers.ItemManager.AddFromTemplate(String itemName, ID templateId, Item destination, ID newItemId) +203
     Sitecore.Data.Managers.ItemManager.AddFromTemplate(String itemName, ID templateId, Item destination) +286
     Sitecore.Data.Items.Item.Add(String name, BranchId branchId) +110
     Sitecore.Workflows.WorkflowContext.AddItem(String name, BranchItem branch, Item parent) +279
     Sitecore.Shell.Framework.Commands.AddMaster.Add(ClientPipelineArgs args) +803
  
  [TargetInvocationException: Exception has been thrown by the target of an invocation.]
     System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
     System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +76
     System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +211
     System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +35
     Sitecore.Nexus.Pipelines.NexusPipelineApi.Resume(PipelineArgs args, Pipeline pipeline) +398
     Sitecore.Web.UI.Sheer.ClientPage.ResumePipeline() +285
     Sitecore.Web.UI.Sheer.ClientPage.OnPreRender(EventArgs e) +547
     Sitecore.Shell.Applications.ContentManager.ContentEditorPage.OnPreRender(EventArgs e) +25
     System.Web.UI.Control.PreRenderRecursiveInternal() +113
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4297
  </code></pre>
  
  <p>A site creates correctly and I see no problems there.</p>
  
  <p>As far as I see the error occurs in the item:addmaster command:</p>
  
  <pre><code>&lt;command name="item:addmaster" type="Sitecore.Shell.Framework.Commands.AddMaster,Sitecore.Kernel" /&gt;
  </code></pre>
  
  <p>I have tried to decompile Sitecore.Nexus library but without any success. After some of hours of the investigation I found that the error disappears if turn off a custom event handler</p>
  
  <pre><code>&lt;event name="item:created"&gt;
     &lt;handler type="App.Client.Tasks.ItemEventHandler, App.Client" method="OnItemCreated" /&gt;
  &lt;/event&gt;
  </code></pre>
  
  <p>The handler is responsible for auto mapping configuration settings in a new site created from a branch. Technically there is an item in branch template called Mapper and it works as a trigger. It is the last item in the branch tree. So when an admin adds a site from a branch, event handler checks that the Mapper is created (in other words a site is created), runs auto mapping functionality and deletes the trigger.</p>
  
  <p>If to omit all checks, the handler looks like</p>
  
  <pre><code>new ItemMappingManager(contextItem, Database.GetDatabase("master").Items[ID.Parse(MappingConfigurationItemId)]).Execute();
  
  // delete the trigger item once the branch has been created and mapping is done
  using (new SecurityDisabler())
  {
      contextItem.Delete();
  }
  </code></pre>
  
  <p>Looks like something changed in the Sitecore's event model but I have a lack of knowledge here.</p>