title: WorkFlow Context System.NullReferenceException
body: <p>in my workflow that insert or remove a contact form a Marketing list. According to an input  boolean parameter value. 
     True-> insert 
                  -> IF contact exists
                                 THEN return
                  -> else
     False -> Remove 
                   -> IF don't exists 
                         THEN return</p>
  
  <p>This WFA runs on incident.  </p>
  
  <p>On the first it fires, it works well, remove or add the contact. But I re-Run changing the Boolean value,  I get the following  exception Msg:  </p>
  
  <blockquote>
    <p>Workflow paused due to error: Unhandled Exception:
    System.NullReferenceException: Object reference not set to an instance
    of an object.    at
    ContactToMList.ContactToMList.Execute(CodeActivityContext
    executionContext)    at
    System.Activities.CodeActivity.InternalExecute(ActivityInstance
    instance, ActivityExecutor executor, BookmarkManager bookmarkManager) 
    at
    System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor </p>
    
    <p>executor, BookmarkManager bookmarkManager, Location resultLocation)</p>
  </blockquote>
  
  <p>It seems like does't take the context. </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Microsoft.Crm.Sdk.Messages;
  using Microsoft.Xrm.Sdk;
  using Microsoft.Xrm.Sdk.Workflow;
  using System.Activities;
  using ContactToMList;
  
  namespace ContactToMList
  {
     public class ContactToMList : CodeActivity
      {
  
  
          [Input("Contatto")]
          [ReferenceTarget("contact")]
          public InArgument&lt;EntityReference&gt; contact { get; set; }
  
          [Input("Marketing List")]
          [ReferenceTarget("list")]
          public InArgument&lt;EntityReference&gt; MList { get; set; }
  
          [Input("Inserimento")]
          public InArgument&lt;bool&gt; inserimento { get; set; }
  
          bool action = false;
          private static string _logs = string.Empty; 
          private static IOrganizationService myService = null;
          private static string _separataore = "\r\n"; 
          private static Log_Entity log = new Log_Entity(string.Empty, myService);
          protected override void Execute(CodeActivityContext executionContext)
          {
              try
              {
  
                  ITracingService tracingService = executionContext.GetExtension&lt;ITracingService&gt;();
  
                  // Create the context
                  IWorkflowContext context = executionContext.GetExtension&lt;IWorkflowContext&gt;();
  
                  IOrganizationServiceFactory serviceFactory = executionContext.GetExtension&lt;IOrganizationServiceFactory&gt;();
  
                  // Create the Organiztion service
                  IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                  myService = service;
                  log.WriteLog("");
                  // Get the target entity from the context
                 // var target = context.InputParameters["EntityId"];
  
  
                  Guid contactiId = contact.Get&lt;EntityReference&gt;(executionContext).Id;
                  Guid ListId = MList.Get&lt;EntityReference&gt;(executionContext).Id;
                  bool insert = inserimento.Get&lt;bool&gt;(executionContext);
  
  
                  // Prepare DataContext by using AutoGenerated cs file
                  XrmDataContext datacontext = new XrmDataContext(service);
  
                  var MyContact = (from c in datacontext.ContactSet where c.ContactId == contactiId select c.Id).ToArray();
                  var MyList = (from l in datacontext.ListSet where l.Id == ListId select l).ToList().FirstOrDefault();
                  // tutti i membri della lista di marketing
                  var members = (from m in datacontext.ListMemberSet where m.ListId.Id == MyList.ListId select m.EntityId.Id).ToArray();
  
  
                  if (MyList.MemberType == 2)
                  {
                      if (MyList.Type.Value == false)
                      {
                          foreach (Guid id in members)
                              if (MyContact.FirstOrDefault() == id)
                                  action = true;
  
  
  
                          if (insert &amp;&amp; !action)
                          {
  
                              AddListMembersListRequest AddMemberRequest = new AddListMembersListRequest();
  
                              AddMemberRequest.ListId = ListId;
                              AddMemberRequest.MemberIds = MyContact;
                              // Use AddListMembersListReponse to get information about the request execution 
                              AddListMembersListResponse AddMemberResponse = service.Execute(AddMemberRequest) as AddListMembersListResponse;
                              //service.Update(MyList);
  
                          }
                          else if (!insert &amp;&amp; action)
                          {
  
                              RemoveMemberListRequest RemoveMemberRequest = new RemoveMemberListRequest();
                              RemoveMemberRequest.ListId = ListId;
                              RemoveMemberRequest.EntityId = MyContact.FirstOrDefault();
                              // Use AddListMembersListReponse to get information about the request execution 
                              RemoveMemberListResponse RemoveMemberResponse = service.Execute(RemoveMemberRequest) as RemoveMemberListResponse;
                              // service.Update(MyList);
  
                          }
                      } 
                      else
                      {
                          tracingService.Trace("Stai cercando di inserire un contatto in una lista di Marketing che non è statica.");
                          _logs += "Stai cercando di inserire un contatto in una lista di Marketing che non è statica." + _separataore;
                          return; 
  
                      }
                  }
                  else
                  {
                      tracingService.Trace("Stai cercando di inserire un enittà diversa da un contatto.");
                      _logs += "Stai cercando di inserire un enittà diversa da un contatto." + _separataore;
                      return;
                  }
  
              }
              catch (Exception ex)
              {
                  log.WriteLog(ex.Message);
  
              }
  
          }
      }
  }
  </code></pre>