'Activate feature' step error :Object Reference not set to an instance of an object

与世无争的帅哥 提交于 2019-12-24 17:07:16

问题


I created a custom timer job and implemented it through a feature event in sharepoint.

when i tried to activate it, its showing this particular error:

Object reference not set to an instance of an object.   at TimerJobExample.MyFeatureReceiver.FeatureActivated(SPFeatureReceiverProperties properties) in C:\Documents and Settings\admin-shuklag.INFRADEV\My Documents\Visual Studio 2008\Projects\TimerJobExample\TimerJobExample\MyFeatureReceiver.cs:line 22 
   at Microsoft.SharePoint.SPFeature.DoActivationCallout(Boolean fActivate, Boolean fForce) 
   at Microsoft.SharePoint.SPFeature.Activate(SPSite siteParent, SPWeb webParent, SPFeaturePropertyCollection props, Boolean fForce) 
   at Microsoft.SharePoint.SPFeatureCollection.AddInternal(Guid featureId, SPFeaturePropertyCollection properties, Boolean force, Boolean fMarkOnly) 
   at Microsoft.SharePoint.SPFeatureCollection.Add(Guid featureId) 
   at Microsoft.SharePoint.WebControls.FeatureActivator.BtnActivateFeature_Click(Object objSender, EventArgs evtargs) 
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e) 
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) 
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

The error line is highlighted below

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {

        SPSite site = properties.Feature.Parent as SPSite;

        // make sure the job isn't already registered


//error line
      **foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)**

        {

            if (job.Name == List_JOB_NAME)

                job.Delete();

        }



        SampleTimer listLoggerJob = new SampleTimer(List_JOB_NAME, site.WebApplication);

        SPMinuteSchedule schedule = new SPMinuteSchedule();

        schedule.BeginSecond = 0;

        schedule.EndSecond = 59;

        schedule.Interval = 5;

        listLoggerJob.Schedule = schedule;

        listLoggerJob.Update();

    }

what could be the problem?


回答1:


Based on your feature receiver, I can see only two possibilities:

  1. Double check that the Scope attribute in feature.xml is set to Site. If it is set to Web, you will get an Object Reference not set exception when trying to use your site variable.
  2. As Janis said, look inside the MyCustomJob constructor. Make sure that all variables are properly initialized.



回答2:


The problem was with the scope of the feature. It was set to Web, and for a feature to be activated at this level, u need an admin account or an account with elevated privileges.

You can either add the application pool id to farm admin accounts or run the code with elevated rights.



来源:https://stackoverflow.com/questions/7443280/activate-feature-step-error-object-reference-not-set-to-an-instance-of-an-obj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!