title: Error While Attempting to Use Custom Events
body: <p>I am trying to use the information from the <a href="http://www.codeproject.com/Articles/17371/Passing-Data-between-Windows-Forms" rel="nofollow">Passing-Data-between-Windows-Forms</a> white paper to write some custom events to pass information between forms.<br>
  Unfortunately, I keep getting a <code>NullReferenceException</code> when I attempt to raise the event.  Below is a scaled down version of the relevant code that I have currently attempted.<br>
  Can someone please have a look and let me know if I am missing something?<br>
  BTW, I am using DevExpress forms if that makes a difference.  I only included the class that is generating my custom event since that is where the code is failing.  On the line where the <code>NullReferenceException</code> occurs, I have verified that the item is not <code>null</code>.</p>
  
  <pre><code>// Class that generates custom event
  public partial class DiscountItemControl : DevExpress.XtraEditors.XtraUserControl
  {
      // Add a delegate
      public delegate void ItemInsertedEventHandler(object sender, ItemInsertedEventArgs e);
  
      // AddCustomerForm an event of the delegate type
      public event ItemInsertedEventHandler ItemInsertedEvent;
  
      public void SaveAndClose()
      {
          // setup event args
          ItemInsertedEventArgs args = new ItemInsertedEventArgs(currentDiscountItem);
  
          // ********** THIS THROWS NullReferenceException *********
          // raise the event to notify listeners
          ItemInsertedEvent(this, args);
  
          this.Dispose();
      }
  }
  
  // Event arguments for event
  public class ItemInsertedEventArgs : System.EventArgs
  {
      private Item item;
  
      public ItemInsertedEventArgs(Item item)
      {
          this.item = item;
      }
  
      public Item InsertedItem
      {
          get
          {
              return this.item;
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>System.NullReferenceException was unhandled by user code   Message="Object reference not set to an instance of an object."   Source="PureService"   StackTrace:
  at MarineService.Tests.DiscountItemControl.SaveAndClose(Boolean&amp; result) in C:\Aaron\Dev\HIGH PRIORITY\ServiceModule\MarineService\ServiceModule\UtilityClasses\UserControls\Items\DiscountItemControl.cs:line 336
  at MarineService.Tests.AddEditItemForm.btnSaveAndClose_Click(Object sender, EventArgs e) in C:\Aaron\Dev\HIGH PRIORITY\ServiceModule\MarineService\ServiceModule\AddEditItemForm.cs:line 326
  at System.Windows.Forms.Control.OnClick(EventArgs e)
  at DevExpress.XtraEditors.BaseButton.OnClick(EventArgs e)
  at DevExpress.XtraEditors.BaseButton.OnMouseUp(MouseEventArgs e)
  at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
  at System.Windows.Forms.Control.WndProc(Message&amp; m)
  at DevExpress.Utils.Controls.ControlBase.WndProc(Message&amp; m)
  at DevExpress.XtraEditors.BaseControl.WndProc(Message&amp; msg)
  at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
  at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
  at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)   InnerException:
  </code></pre>