Debugging Custom Event Receivers.

Here’s a weird error that took some time to figure out. Generic “Object reference not set to an instance of an object.” Error on a custom SharePoint 2010 event receiver. If I went through the UI to change a field on a list, the event receiver fires fine. But, if a workflow changed the field it threw the error below. I finally found if I attached the OWSTimer process to Visual Studio, it would debug, but still not show the exact problem. The solution is to find the Log On account for OWSTimer in the Services console (services.msc) and then to grant that account permissions on the list that the event receiver is running on.

Edit -checking the permissions of the OWSTimer account is just one possible solution.  What I found out was that the list was hanging on to old event receivers as well.  I had to use SPManager to see how many event receivers were still active on the list.

Also, I thought the  OWSTimer process may be holding onto cached dll’s, so you will need to restart the OWSTimer service in services.msc, and IIS Reset to reset the app pools in order to see new dll changes.

After starting over, checking the number of event receivers, attaching to the W3 process for UI changes, and the OWSTimer for timer based changes, I was able to get a stable debugging environment.  I also reset the OWSTimer service and IIS between each debugging session.

 

Message:

Object reference not set to an instance of an object.

Stack Trace:

at BusinessSolutions.SharePoint.EventReceivers.PricingRequestItemUpdate.PricingRequestReceiver.

PricingRequestReceiver.<>c__DisplayClass2.<UpdateDashboardItem>b__0()
at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)
at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
at BusinessSolutions.SharePoint.EventReceivers.PricingRequestItemUpdate.PricingRequestReceiver.

PricingRequestReceiver.UpdateDashboardItem(SPItemEventProperties properties, Boolean bNewItem)
at BusinessSolutions.SharePoint.EventReceivers.PricingRequestItemUpdate.PricingRequestReceiver.

PricingRequestReceiver.ItemUpdating(SPItemEventProperties properties)