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. Event Exception

Event Exception

Scheduled Pinned Locked Moved C#
question
4 Posts 3 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.
  • J Offline
    J Offline
    Justin Perez
    wrote on last edited by
    #1

    I have an event that needs to occur every time a file containing records is imported, I keep getting an exception at this line: OnImportFile(this, new ImportFileEventArgs(fi.Length, i, fi[i].Name)); The exception is "Object reference not set to an instance of an object." I have check to make sure none of my variables are Null, I don't know what the deal is. Suggestions Please? :) J.Perez

    C B 2 Replies Last reply
    0
    • J Justin Perez

      I have an event that needs to occur every time a file containing records is imported, I keep getting an exception at this line: OnImportFile(this, new ImportFileEventArgs(fi.Length, i, fi[i].Name)); The exception is "Object reference not set to an instance of an object." I have check to make sure none of my variables are Null, I don't know what the deal is. Suggestions Please? :) J.Perez

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Justin Perez wrote:

      I have check to make sure none of my variables are Null, I don't know what the deal is.

      :confused: You just explained what the deal is, then you tell us you don't know what the deal is. Perhaps you are not explaining yourself very well.

      Justin Perez wrote:

      OnImportFile(this, new ImportFileEventArgs(fi.Length, i, fi[i].Name));

      TIP: Don't put all this stuff in one line of code. It makes the code difficult to read, and difficult to debug. Change it to:

      int length = fi.Length;
      FIClassName currentFI = fi[i];
      string fiName = currentFI.Name;
      ImportFileEventArgs(length, i, fiName);

      That makes it a lot easier to debug and step through and see where all the values are and how it is being built up.


      Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

      1 Reply Last reply
      0
      • J Justin Perez

        I have an event that needs to occur every time a file containing records is imported, I keep getting an exception at this line: OnImportFile(this, new ImportFileEventArgs(fi.Length, i, fi[i].Name)); The exception is "Object reference not set to an instance of an object." I have check to make sure none of my variables are Null, I don't know what the deal is. Suggestions Please? :) J.Perez

        B Offline
        B Offline
        bearfx
        wrote on last edited by
        #3

        Is anything actually assigned to handle the OnImportFile? someObject.OnImportFile+= new OnImportFileEvent(someHandler); If not, you will get an exception Take this example code class Program { static void Main(string[] args) { someClass obj = new someClass(); //Exception Here obj.RaiseEvent(); obj.someEvent += new someClass.SomeEvent(EventHandler); //No Exception obj.RaiseEvent(); } static void EventHandler(object sender, EventArgs e) { } } class someClass { public delegate void SomeEvent(object sender, EventArgs args); public SomeEvent someEvent; public void RaiseEvent() { someEvent(this,new EventArgs()); } } You can resolve this by testing someEvent for null public void RaiseEvent() { if(someEvent!=null) someEvent(this,new EventArgs()); } or setting it to have at least one handler when you create the object class someClass { public delegate void SomeEvent(object sender, EventArgs args); public SomeEvent someEvent; someClass{ someEvent+=new SomeEvent(internalHandler); } private void internalHandler(object sender, EventArgs e) {} public void RaiseEvent() { someEvent(this,new EventArgs()); } } I usually test for null, but either works.

        J 1 Reply Last reply
        0
        • B bearfx

          Is anything actually assigned to handle the OnImportFile? someObject.OnImportFile+= new OnImportFileEvent(someHandler); If not, you will get an exception Take this example code class Program { static void Main(string[] args) { someClass obj = new someClass(); //Exception Here obj.RaiseEvent(); obj.someEvent += new someClass.SomeEvent(EventHandler); //No Exception obj.RaiseEvent(); } static void EventHandler(object sender, EventArgs e) { } } class someClass { public delegate void SomeEvent(object sender, EventArgs args); public SomeEvent someEvent; public void RaiseEvent() { someEvent(this,new EventArgs()); } } You can resolve this by testing someEvent for null public void RaiseEvent() { if(someEvent!=null) someEvent(this,new EventArgs()); } or setting it to have at least one handler when you create the object class someClass { public delegate void SomeEvent(object sender, EventArgs args); public SomeEvent someEvent; someClass{ someEvent+=new SomeEvent(internalHandler); } private void internalHandler(object sender, EventArgs e) {} public void RaiseEvent() { someEvent(this,new EventArgs()); } } I usually test for null, but either works.

          J Offline
          J Offline
          Justin Perez
          wrote on last edited by
          #4

          bearfx wrote:

          Is anything actually assigned to handle the OnImportFile? someObject.OnImportFile+= new OnImportFileEvent(someHandler);

          I didn't not have anything assigned to handle OnImportFile. I am new to events so bear with me here :) Below is my delegate and event in my class. public delegate void GNISImportFileEventHandler(object sender, ImportFileEventArgs e); public event GNISImportFileEventHandler OnImportFile; So to assign something on handle OnImportFile I have this: OnImportFile += new GNISImportFileEventHandler(OnImportFile); From the articles I have been reading that should do the trick, but I get the exception "Delegate to an instance method cannot have null 'this'." A article that covers this issue would be greatly appreciated.

          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