Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:10:52+00:00 2026-06-17T21:10:52+00:00

I am trying to use the information from the Passing-Data-between-Windows-Forms white paper to write

  • 0

I am trying to use the information from the Passing-Data-between-Windows-Forms white paper to write some custom events to pass information between forms.
Unfortunately, I keep getting a NullReferenceException when I attempt to raise the event. Below is a scaled down version of the relevant code that I have currently attempted.
Can someone please have a look and let me know if I am missing something?
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 NullReferenceException occurs, I have verified that the item is not null.

// 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;
        }
    }
}

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& 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& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at DevExpress.Utils.Controls.ControlBase.WndProc(Message& m)
at DevExpress.XtraEditors.BaseControl.WndProc(Message& msg)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)   InnerException:
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:10:54+00:00Added an answer on June 17, 2026 at 9:10 pm

    You don’t have any event handlers attached. Usually custom event code has a check for that:

    ItemInsertedEventHandler handler = this.ItemInsertedEvent;
    if(handler != null) handler(this, args);
    

    You need to have some code somewhere that adds a handler, i.e.

    MyObject.ItemInsertedEvent += myHandler;
    

    EDIT: Jon Skeet is right about race condition issues, you should use the local variable like he suggests. I’ve changed my example to match.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to use jQuery to grab the information from $_POST and return
I am trying to use python's telnetlib module to get information from a remote
I have been trying to use the document.getElementByID to pull information from an HTML
I am trying to build a Recommendation system that could use the information from
I was trying to use http://www.jongsma.org/gc/scripts/ofx-ba.py to grab my bank account information from wachovia.
I trying to use ImageInfo and Jython to get information from a image on
I'm trying to use JSON, I'm getting the information from the server so I've
I'm trying to retrieve some information from my onActivityResult method, in my case is
Here is my basic situation. I'm trying to use NHibernate to get information from
I'm trying to use a MySQL database to save player information, but whenever there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.