Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Custom Event Throwing Null When Called?

Custom Event Throwing Null When Called?

Scheduled Pinned Locked Moved C#
helpquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Abydosgater

    Hi, Im trying to define some custom events within one of my classes. I have set the delegate and the event as follows: public delegate void SegmentProgressUpdate(); public event SegmentProgressUpdate ProgressUpdate; That is just above my constructor. It compiles, But at runtime when the event gets called I get the "Object reference not set to an instance of an object." Error. Im sure i am doing something wrong, But i cant see what. I have read a dozen tutorials on custom events online, that all only have what i have above. And i have googled the error related to events but cant find a solution that works. Does anyone have any idea?

    G Offline
    G Offline
    Gareth H
    wrote on last edited by
    #2

    Abydosgater, I guessing you don't listen to the event. When you fire the event, check its not null.

    if (ProgressUpdate != null)
    ProgressUpdate();

    Also, its best to try and stick to .net conventions when possible, so your delegate should take 2 params.

    public delegate void SegmentProgressUpdate(object sender, EventArgs e);

    Sender is the object calling the event, and the event args is the class with the information you'd like to pass, if you don't pass anything, just do a: EventArgs.Empty

    Regards, Gareth. (FKA gareth111)

    A 1 Reply Last reply
    0
    • G Gareth H

      Abydosgater, I guessing you don't listen to the event. When you fire the event, check its not null.

      if (ProgressUpdate != null)
      ProgressUpdate();

      Also, its best to try and stick to .net conventions when possible, so your delegate should take 2 params.

      public delegate void SegmentProgressUpdate(object sender, EventArgs e);

      Sender is the object calling the event, and the event args is the class with the information you'd like to pass, if you don't pass anything, just do a: EventArgs.Empty

      Regards, Gareth. (FKA gareth111)

      A Offline
      A Offline
      Abydosgater
      wrote on last edited by
      #3

      Thanks for the quick reply Gareth. I have tried this, and it still doesnt work. It doesnt crash with the error but as you will see: SegmentOne.ProgressUpdate += new Segment.SegmentProgressUpdate(SegmentOne_ProgressUpdate); Im listening to the event, Thats in my main application, And i have changed it to this: if (ProgressUpdate != null) { ProgressUpdate(this, System.EventArgs.Empty); } else System.Windows.Forms.MessageBox.Show("error segment.cs:109"); But now all it does is throw the error message box.

      G 1 Reply Last reply
      0
      • A Abydosgater

        Thanks for the quick reply Gareth. I have tried this, and it still doesnt work. It doesnt crash with the error but as you will see: SegmentOne.ProgressUpdate += new Segment.SegmentProgressUpdate(SegmentOne_ProgressUpdate); Im listening to the event, Thats in my main application, And i have changed it to this: if (ProgressUpdate != null) { ProgressUpdate(this, System.EventArgs.Empty); } else System.Windows.Forms.MessageBox.Show("error segment.cs:109"); But now all it does is throw the error message box.

        G Offline
        G Offline
        Gareth H
        wrote on last edited by
        #4

        Abydosgater, When do you listen to the event?, once you've created SegmentOne? or...?

        Regards, Gareth. (FKA gareth111)

        A 1 Reply Last reply
        0
        • G Gareth H

          Abydosgater, When do you listen to the event?, once you've created SegmentOne? or...?

          Regards, Gareth. (FKA gareth111)

          A Offline
          A Offline
          Abydosgater
          wrote on last edited by
          #5

          Yes once i have created the SegmentOne Object. SegmentOne = new Segment(SegmentOneStart, SegmentOneEnd, this._webRequest); SegmentOne.ProgressUpdate += new Segment.SegmentProgressUpdate(SegmentOne_ProgressUpdate); Hmm

          1 Reply Last reply
          0
          • A Abydosgater

            Hi, Im trying to define some custom events within one of my classes. I have set the delegate and the event as follows: public delegate void SegmentProgressUpdate(); public event SegmentProgressUpdate ProgressUpdate; That is just above my constructor. It compiles, But at runtime when the event gets called I get the "Object reference not set to an instance of an object." Error. Im sure i am doing something wrong, But i cant see what. I have read a dozen tutorials on custom events online, that all only have what i have above. And i have googled the error related to events but cant find a solution that works. Does anyone have any idea?

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #6

            You've got something screwy going on somewhere in your code. This is a simple sample that has 2 events - one with custom event args.

            public class TestA
            {
                public event EventHandler Property1Changed;
                public event EventHandler<TestEventArgs> Property2Changed;
            
                public int Property1
                {
                    set { OnProperty1Changed(); }
                }
                public int Property2
                {
                    set { OnProperty2Changed(value); }
                }
                protected virtual void OnProperty1Changed()
                {
                    if(Property1Changed!=null)
                        Property1Changed(this, EventArgs.Empty);
                }
                protected virtual void OnProperty2Changed(int value)
                {
                    if (Property2Changed != null)
                        Property2Changed(this, new TestEventArgs(value));
                }
            }
            public class TestEventArgs : EventArgs
            {
                public TestEventArgs(int value)
                {
                    m\_Value = value;
                }
                private int m\_Value;
                public int Value
                {
                    get { return m\_Value; }
                }
            }
            

            You can instanciate, subscribe to the events and set the properties to raise the events like:

            TestA testA = new TestA();
            testA.Property1Changed += new EventHandler(testA_Property1Changed);
            testA.Property2Changed += new EventHandler<TestEventArgs>(testA_Property2Changed);
            testA.Property1 = 1;
            testA.Property2 = 2;

            then the methods called above

                void testA\_Property2Changed(object sender, TestEventArgs e)
                {
                    Console.WriteLine("Property 2 Changed to: " + e.Value);
                }
            
                void testA\_Property1Changed(object sender, EventArgs e)
                {
                    Console.WriteLine("Property 1 Changed.");
                }
            

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

            1 Reply Last reply
            0
            • A Abydosgater

              Hi, Im trying to define some custom events within one of my classes. I have set the delegate and the event as follows: public delegate void SegmentProgressUpdate(); public event SegmentProgressUpdate ProgressUpdate; That is just above my constructor. It compiles, But at runtime when the event gets called I get the "Object reference not set to an instance of an object." Error. Im sure i am doing something wrong, But i cant see what. I have read a dozen tutorials on custom events online, that all only have what i have above. And i have googled the error related to events but cant find a solution that works. Does anyone have any idea?

              A Offline
              A Offline
              Abydosgater
              wrote on last edited by
              #7

              Ive taken a look at that example, and i have added a full new test event to try it using the code above as an example. //Above my constructor public event EventHandler TestEvent; //within my constructor (in a loop) if (TestEvent != null) TestEvent(this, System.EventArgs.Empty); else System.Windows.Forms.MessageBox.Show("Event returned null"); //And in my main app when i use the class: SegmentOne = new Segment(SegmentOneStart, SegmentOneEnd, this._webRequest); SegmentOne.TestEvent += new EventHandler(SegmentOne_TestEvent); But as before, its just returning that the event is null and not calling it. even though i am listening for it.

              D 2 Replies Last reply
              0
              • A Abydosgater

                Ive taken a look at that example, and i have added a full new test event to try it using the code above as an example. //Above my constructor public event EventHandler TestEvent; //within my constructor (in a loop) if (TestEvent != null) TestEvent(this, System.EventArgs.Empty); else System.Windows.Forms.MessageBox.Show("Event returned null"); //And in my main app when i use the class: SegmentOne = new Segment(SegmentOneStart, SegmentOneEnd, this._webRequest); SegmentOne.TestEvent += new EventHandler(SegmentOne_TestEvent); But as before, its just returning that the event is null and not calling it. even though i am listening for it.

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #8

                Abydosgater wrote:

                within my constructor

                If it's in the constructor - then the object instance may not yet created, and even if it is, the listener SegmentOne.TestEvent += new ... has not yet been attached so it will be always be null at that point.

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                1 Reply Last reply
                0
                • A Abydosgater

                  Hi, Im trying to define some custom events within one of my classes. I have set the delegate and the event as follows: public delegate void SegmentProgressUpdate(); public event SegmentProgressUpdate ProgressUpdate; That is just above my constructor. It compiles, But at runtime when the event gets called I get the "Object reference not set to an instance of an object." Error. Im sure i am doing something wrong, But i cant see what. I have read a dozen tutorials on custom events online, that all only have what i have above. And i have googled the error related to events but cant find a solution that works. Does anyone have any idea?

                  G Offline
                  G Offline
                  Giorgi Dalakishvili
                  wrote on last edited by
                  #9

                  C# Event Implementation Fundamentals, Best Practices and Conventions[^]

                  Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion

                  1 Reply Last reply
                  0
                  • A Abydosgater

                    Ive taken a look at that example, and i have added a full new test event to try it using the code above as an example. //Above my constructor public event EventHandler TestEvent; //within my constructor (in a loop) if (TestEvent != null) TestEvent(this, System.EventArgs.Empty); else System.Windows.Forms.MessageBox.Show("Event returned null"); //And in my main app when i use the class: SegmentOne = new Segment(SegmentOneStart, SegmentOneEnd, this._webRequest); SegmentOne.TestEvent += new EventHandler(SegmentOne_TestEvent); But as before, its just returning that the event is null and not calling it. even though i am listening for it.

                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #10

                    I think the only way you can raise an event in the constructor that can be listened to is using a static event.

                    public class TestA
                    {
                    public static event EventHandler ConstructorCompleted;
                    public TestA()
                    {
                    OnConstructorCompleted(this);
                    }
                    protected static void OnConstructorCompleted(TestA instance)
                    {
                    if (ConstructorCompleted != null)
                    ConstructorCompleted(instance, EventArgs.Empty);
                    }
                    }

                    TestA.ConstructorCompleted += new EventHandler(TestA_ConstructorCompleted);
                    TestA testA = new TestA();

                    void TestA_ConstructorCompleted(object sender, EventArgs e)
                    {
                    if (sender is TestA)
                    {
                    Console.WriteLine("OK");
                    TestA.ConstructorCompleted -= TestA_ConstructorCompleted;
                    }
                    }

                    Dave
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                    1 Reply Last reply
                    0
                    • A Abydosgater

                      Hi, Im trying to define some custom events within one of my classes. I have set the delegate and the event as follows: public delegate void SegmentProgressUpdate(); public event SegmentProgressUpdate ProgressUpdate; That is just above my constructor. It compiles, But at runtime when the event gets called I get the "Object reference not set to an instance of an object." Error. Im sure i am doing something wrong, But i cant see what. I have read a dozen tutorials on custom events online, that all only have what i have above. And i have googled the error related to events but cant find a solution that works. Does anyone have any idea?

                      A Offline
                      A Offline
                      Abydosgater
                      wrote on last edited by
                      #11

                      Thanks to Davie and Giorgi Dalakishvili i figured out the issue. As Davie said above, It was a loop in the constructor so when it was getting called it had not yet been set to listen to. So i moved the loop to its own Start() and it now works as below: SegmentOne = new Segment(SegmentOneStart, SegmentOneEnd, this._webRequest); SegmentOne.ProgressUpdate += new Segment.SegmentProgressUpdate(SegmentOne_ProgressUpdate); SegmentOne.Start(); Thanks guys

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups