Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am creating a TimerJob that invokes the workflow. Refer to the Execute method

    public override void Execute(Guid targetInstanceId)
    {
            var reader = new ConfigReader(this.configPath);
            var config = reader.GetConfiguration();

            using (var siteCollection = new SPSite(config.SiteURL))
            using (var site = siteCollection.RootWeb)
            {
                var taskList = site.Lists[config.ListName];
                var manager = taskList.ParentWeb.Site.WorkflowManager;
                var wfAssociation = taskList.WorkflowAssociations.GetAssociationByName(
                        config.WorkflowName,
                        System.Globalization.CultureInfo.CurrentCulture);

                foreach (SPListItem item in taskList.Items)
                {
                    manager.StartWorkflow(item, wfAssociation, wfAssociation.AssociationData, true);
                }
            }
    }

After running the code, a NullReferenceException was thrown. Refer to the stack trace below

System.NullReferenceException: Object reference not set to an instance of an object.
   at
Microsoft.SharePoint.SPList.PrepForWorkflowTemplate(SPWorkflowTemplate
wt)
   at
Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflowElev(SPList
Item item, SPFile file, SPWorkflowAssociation association, SPWorkflowEvent startEvent, Boolean bAutoStart, Boolean bCreateOnly)
   at
Microsoft.SharePoint.Workflow.SPWorkflowManager.<>c__DisplayClass1.<Star
tWorkflow>b__0()
   at Microsoft.SharePoint.SPSecurity.CodeToRunElevatedWrapper(Object
state)
   at
Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivi
leges>b__2()
   at
Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunEle
vated secureCode)
   at
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback
secureCode, Object param)
   at
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunEleva
ted secureCode)
   at
Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflow(SPListItem
item, SPWorkflowAssociation association, SPWorkflowEvent startEvent, Boolean bAutoStart, Boolean bCreateOnly)
   at
Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflow(SPListItem
item, SPWorkflowAssociation association, String eventData, Boolean
isAutoStart)
   at
Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflow(SPListItem
item, SPWorkflowAssociation association, String eventData)
   at Exelon.CorpAppEvents.WorkflowInvokerTimerJob.Execute(Guid
targetInstanceId)

I checked the tasklist, manager, and wfAssociation and none of them are null. Moreover, I granted NT AUTHORITY\authenticated users to check if I have a permission issue, but I still get the same error.

Now, where should I look to resolve the issue. Thanks.

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.