title: Error building a content type in SP 2007
body: <p>I have an event handler that catches an ItemAdding event and changes the content type on the fly. This is my code to build the content type:</p>
  
  <pre><code>SPContentType baseContentType = web.AvailableContentTypes["Folder"];
  SPContentType type = new SPContentType(baseContentType, web.ContentTypes, "Custom Folder");
  
  // Add fields to new content type
  AddField(ref type, "User", "Owner");
  AddField(ref type, "DateTime", "Date");
  AddField(ref type, "Text", "Description");
  AddField(ref type, "Text", "Justification");
  AddField(ref type, "User", "Users");
  
  list.ContentTypes.Add(type);
  list.Update();
  </code></pre>
  
  <p>..snip..</p>
  
  <pre><code>void AddField(ref SPContentType contentType, string fieldType, string fieldName)
  {
      SPField field = new SPField(contentType.Fields, fieldType, fieldName);
      if (fieldName.Equals("Users"))
          ((SPFieldUser)field).AllowMultipleValues = true;
      field.Update();
      contentType.Fields.Add(field); // Exception thrown here
      SPFieldLink fieldLink = new SPFieldLink(contentType.Fields[fieldName]);
      contentType.FieldLinks.Add(fieldLink); 
      contentType.Update();
  }
  </code></pre>
  
  <p>On the line: contentType.FieldLinks.Add(fieldLink);,</p>
  
  <p>I'm getting an ArgumentNullException: value cannot be null. Parameter name: g</p>
  
  <p>Does anyone know what this means or can see what I'm doing wrong? Thanks in advance!</p>
  
  <p>Stack trace:</p>
  
  <p>at Microsoft.SharePoint.SPField.UpdateCore(Boolean bToggleSealed)
  at Microsoft.SharePoint.SPField.Update()
  at ChangeContentTypeEventHandler.ChangeContentTypeEventHandler.AddField(SPContentType&amp; contentType, String fieldType, String fieldName)
     at ChangeContentTypeEventHandler.ChangeContentTypeEventHandler.ItemAdding(SPItemEventProperties properties)</p>