Event Exception
-
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
-
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
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
-
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
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 nullpublic void RaiseEvent() { if(someEvent!=null) someEvent(this,new EventArgs()); }
or setting it to have at least one handler when you create the objectclass 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. -
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 nullpublic void RaiseEvent() { if(someEvent!=null) someEvent(this,new EventArgs()); }
or setting it to have at least one handler when you create the objectclass 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.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.