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 writing code for feature activated code. But when I try to deploy my solution through VS 2010 it says deploy failed Error on feature activation : object reference not set to an instance of an object. But when I run my solution in debug mode there is no error in my code. Any thoughts?

share|improve this question
1  
Please don't ask two times the same question. I've answered here : sharepoint.stackexchange.com/a/39990/3497 –  Steve B Jul 2 '12 at 11:00
    
If you need to update your question in future, please edit the original question rather than creating a second similar one. In the meantime I have merged your two questions for you. –  SPDoctor Jul 2 '12 at 16:49

3 Answers 3

up vote 6 down vote accepted

At the beginning of your FeatureActivated method, put this line of code :

System.Diagnostics.Debugger.Break();

Then, try to activate the feature. This will popup an "exception" window that let you to attach the debugger of your choice.

After attaching, your code will be pause at this line, and you will be able to walk into your code to find the issue.

[Edit] concerning your comments

The event receivers run asynchronously (at least by default). They are not fired under the http request lifecycle, that's why the SPContext.Current property return null. SPContext.Current is wrapper around the HttpContext.Current value (which is null in this case).

Instead, you can use SPFeatureReceiverProperties.Feature.Parent Property to get the SPWeb, SPSite, SPWebApplicaiton or SPFarm object (depending on the scope of the feature).

I've written a small utility class that can help to extract the useful properties :

/// <summary>
/// Provides methods for faciliting feature manipulations / eventing
/// </summary>
public static class FeatureUtilities
{

    /// <summary>
    /// Build a context to be used from <see cref="Microsoft.SharePoint.SPFeatureReceiver.FeatureActivated(Microsoft.SharePoint.SPFeatureReceiverProperties)"/> and 
    /// <see cref="Microsoft.SharePoint.SPFeatureReceiver.FeatureDeactivating(Microsoft.SharePoint.SPFeatureReceiverProperties)"/> handler
    /// of a <see cref="SPFeatureReceiver"/> object and run a code within this context.
    /// </summary>
    /// <param name="properties">The feature activated/deactivation properties.</param>
    /// <param name="code">Code to run contextualized.</param>
    /// <remarks>This method will explorer the properties object to populate the four objects : <see cref="SPWeb"/>,
    /// <see cref="SPSite"/>, <see cref="SPWebApplication"/> and <see cref="SPFarm"/>. After the call to <c>code</c> delegate
    /// the <c>site</c> and <c>web</c> objects are disposed</remarks>
    /// <example>
    /// <![CDATA[
    /// public override void FeatureActivated(SPFeatureReceiverProperties properties)
    /// {
    ///     properties.RunContextualized((web, site, app, farm) =>
    ///     {
    ///         DoSomething(web,site);
    ///     });
    /// }
    /// ]]>
    /// </example>
    /// <exception cref="ArgumentNullException"><c>properties</c> or <c>code</c> is null</exception>
    /// <exception cref="ArgumentException"><c>properties.Feature</c> is null. This is often due to the call of this method
    /// in the Installed or Uninstalled event of the <see cref="SPFeatureReceiver"/>.</exception>
    public static void RunContextualized(SPFeatureReceiverProperties properties, Action<SPWeb, SPSite, SPWebApplication, SPFarm> code)
    {
        if (properties == null) throw new ArgumentNullException("properties");
        if (code == null) throw new ArgumentNullException("code");
        if (properties.Feature == null) throw new ArgumentException("properties.Feature is null. Maybe you are not in the FeatureActivated/FeatureDeactivating methods ?");

        var web = properties.Feature.Parent as SPWeb;
        var site = properties.Feature.Parent as SPSite;
        var app = properties.Feature.Parent as SPWebApplication;
        var farm = properties.Feature.Parent as SPFarm;

        if (web != null)
        {
            site = web.Site;
            app = site.WebApplication;
            farm = app.Farm;
        }
        else if (site != null)
        {
            web = site.RootWeb;
            app = site.WebApplication;
            farm = app.Farm;
        }
        else if (app != null)
        {
            farm = app.Farm;
        }

        using (site)
        {
            using (web)
            {
                code(web, site, app, farm);
            }
        }
    }

    /// <summary>
    /// Build a context to be used from <see cref="Microsoft.SharePoint.SPFeatureReceiver.FeatureActivated(Microsoft.SharePoint.SPFeatureReceiverProperties)"/> and 
    /// <see cref="Microsoft.SharePoint.SPFeatureReceiver.FeatureDeactivating(Microsoft.SharePoint.SPFeatureReceiverProperties)"/> handler
    /// of a <see cref="SPFeatureReceiver"/> object and run a code within this context.
    /// </summary>
    /// <param name="properties">The feature activated/deactivation properties.</param>
    /// <param name="code">Code to run contextualized.</param>
    /// <remarks>This method will explorer the properties object to populate the three objects : <see cref="SPWeb"/>,
    /// <see cref="SPSite"/> and <see cref="SPWebApplication"/>. After the call to <c>code</c> delegate
    /// the <c>site</c> and <c>web</c> objects are disposed</remarks>
    /// <example>
    /// <![CDATA[
    /// public override void FeatureActivated(SPFeatureReceiverProperties properties)
    /// {
    ///     properties.RunContextualized((web, site, app, farm) =>
    ///     {
    ///         DoSomething(web,site);
    ///     });
    /// }
    /// ]]>
    /// </example>
    /// <exception cref="ArgumentNullException"><c>properties</c> or <c>code</c> is null</exception>
    /// <exception cref="ArgumentException"><c>properties.Feature</c> is null. This is often due to the call of this method
    /// in the Installed or Uninstalled event of the <see cref="SPFeatureReceiver"/>.</exception>
    public static void RunContextualized(SPFeatureReceiverProperties properties, Action<SPWeb, SPSite, SPWebApplication> code)
    {
        RunContextualized(properties, (web, site, app, farm) => code(web, site, app));
    }

    /// <summary>
    /// Build a context to be used from <see cref="Microsoft.SharePoint.SPFeatureReceiver.FeatureActivated(Microsoft.SharePoint.SPFeatureReceiverProperties)"/> and 
    /// <see cref="Microsoft.SharePoint.SPFeatureReceiver.FeatureDeactivating(Microsoft.SharePoint.SPFeatureReceiverProperties)"/> handler
    /// of a <see cref="SPFeatureReceiver"/> object and run a code within this context.
    /// </summary>
    /// <param name="properties">The feature activated/deactivation properties.</param>
    /// <param name="code">Code to run contextualized.</param>
    /// <remarks>This method will explorer the properties object to populate both <see cref="SPWeb"/> and
    /// <see cref="SPSite"/> objects. After the call to <c>code</c> delegate
    /// the <c>site</c> and <c>web</c> objects are disposed</remarks>
    /// <example>
    /// <![CDATA[
    /// public override void FeatureActivated(SPFeatureReceiverProperties properties)
    /// {
    ///     properties.RunContextualized((web, site, app, farm) =>
    ///     {
    ///         DoSomething(web,site);
    ///     });
    /// }
    /// ]]>
    /// </example>
    /// <exception cref="ArgumentNullException"><c>properties</c> or <c>code</c> is null</exception>
    /// <exception cref="ArgumentException"><c>properties.Feature</c> is null. This is often due to the call of this method
    /// in the Installed or Uninstalled event of the <see cref="SPFeatureReceiver"/>.</exception>
    public static void RunContextualized(SPFeatureReceiverProperties properties, Action<SPWeb, SPSite> code)
    {
        RunContextualized(properties, (web, site, app, farm) => code(web, site));
    }

}

And the corresponding extension methods :

/// <summary>
/// Provides extentions methods for faciliting feature manipulations / eventing
/// </summary>
public static class SPFeatureReceiverPropertiesExtensions
{
    /// <summary>
    /// Build a context to be used from <see cref="Microsoft.SharePoint.SPFeatureReceiver.FeatureActivated(Microsoft.SharePoint.SPFeatureReceiverProperties)"/> and 
    /// <see cref="Microsoft.SharePoint.SPFeatureReceiver.FeatureDeactivating(Microsoft.SharePoint.SPFeatureReceiverProperties)"/> handler
    /// of a <see cref="SPFeatureReceiver"/> object and run a code within this context.
    /// </summary>
    /// <param name="properties">The feature activated/deactivation properties.</param>
    /// <param name="code">Code to run contextualized.</param>
    /// <remarks>This method will explorer the properties object to populate the four objects : <see cref="SPWeb"/>,
    /// <see cref="SPSite"/>, <see cref="SPWebApplication"/> and <see cref="SPFarm"/>. After the call to <c>code</c> delegate
    /// the <c>site</c> and <c>web</c> objects are disposed</remarks>
    /// <example>
    /// <![CDATA[
    /// public override void FeatureActivated(SPFeatureReceiverProperties properties)
    /// {
    ///     properties.RunContextualized((web, site, app, farm) =>
    ///     {
    ///         DoSomething(web,site);
    ///     });
    /// }
    /// ]]>
    /// </example>
    /// <exception cref="ArgumentNullException"><c>properties</c> or <c>code</c> is null</exception>
    /// <exception cref="ArgumentException"><c>properties.Feature</c> is null. This is often due to the call of this method
    /// in the Installed or Uninstalled event of the <see cref="SPFeatureReceiver"/>.</exception>
    public static void RunContextualized(this SPFeatureReceiverProperties properties, Action<SPWeb, SPSite, SPWebApplication, SPFarm> code)
    {
        FeatureUtilities.RunContextualized(properties, code);
    }

    /// <summary>
    /// Build a context to be used from <see cref="Microsoft.SharePoint.SPFeatureReceiver.FeatureActivated(Microsoft.SharePoint.SPFeatureReceiverProperties)"/> and 
    /// <see cref="Microsoft.SharePoint.SPFeatureReceiver.FeatureDeactivating(Microsoft.SharePoint.SPFeatureReceiverProperties)"/> handler
    /// of a <see cref="SPFeatureReceiver"/> object and run a code within this context.
    /// </summary>
    /// <param name="properties">The feature activated/deactivation properties.</param>
    /// <param name="code">Code to run contextualized.</param>
    /// <remarks>This method will explorer the properties object to populate the three objects : <see cref="SPWeb"/>,
    /// <see cref="SPSite"/> and <see cref="SPWebApplication"/>. After the call to <c>code</c> delegate
    /// the <c>site</c> and <c>web</c> objects are disposed</remarks>
    /// <example>
    /// <![CDATA[
    /// public override void FeatureActivated(SPFeatureReceiverProperties properties)
    /// {
    ///     properties.RunContextualized((web, site, app, farm) =>
    ///     {
    ///         DoSomething(web,site);
    ///     });
    /// }
    /// ]]>
    /// </example>
    /// <exception cref="ArgumentNullException"><c>properties</c> or <c>code</c> is null</exception>
    /// <exception cref="ArgumentException"><c>properties.Feature</c> is null. This is often due to the call of this method
    /// in the Installed or Uninstalled event of the <see cref="SPFeatureReceiver"/>.</exception>
    public static void RunContextualized(this SPFeatureReceiverProperties properties, Action<SPWeb, SPSite, SPWebApplication> code)
    {
        FeatureUtilities.RunContextualized(properties, code);
    }

    /// <summary>
    /// Build a context to be used from <see cref="Microsoft.SharePoint.SPFeatureReceiver.FeatureActivated(Microsoft.SharePoint.SPFeatureReceiverProperties)"/> and
    /// <see cref="Microsoft.SharePoint.SPFeatureReceiver.FeatureDeactivating(Microsoft.SharePoint.SPFeatureReceiverProperties)"/> handler
    /// of a <see cref="SPFeatureReceiver"/> object and run a code within this context.
    /// </summary>
    /// <param name="properties">The feature activated/deactivation properties.</param>
    /// <param name="code">Code to run contextualized.</param>
    /// <remarks>This method will explorer the properties object to populate both <see cref="SPWeb"/> and
    /// <see cref="SPSite"/> objects. After the call to <c>code</c> delegate
    /// the <c>site</c> and <c>web</c> objects are disposed</remarks>
    /// <example>
    /// <![CDATA[
    /// public override void FeatureActivated(SPFeatureReceiverProperties properties)
    /// {
    ///     properties.RunContextualized((web, site, app, farm) =>
    ///     {
    ///         DoSomething(web,site);
    ///     });
    /// }
    /// ]]>
    /// </example>
    /// <exception cref="ArgumentNullException"><c>properties</c> or <c>code</c> is null</exception>
    /// <exception cref="ArgumentException"><c>properties.Feature</c> is null. This is often due to the call of this method
    /// in the Installed or Uninstalled event of the <see cref="SPFeatureReceiver"/>.</exception>
    public static void RunContextualized(this SPFeatureReceiverProperties properties, Action<SPWeb, SPSite> code)
    {
        FeatureUtilities.RunContextualized(properties, code);
    }

The usage of this class is :

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
         properties.RunContextualized((web, site) =>
         {
             DoSomething(web,site);
         });
    }

This simplifies the process of analyzing the feature context.

share|improve this answer
    
I can debug my feature code.But then there is no error. –  V.B Jul 2 '12 at 9:31
    
The idea is to reproduce the error under classic activation, not VS F5 key press. –  Steve B Jul 2 '12 at 9:37
    
Great i see the the problem. I am using SConext.Current and then spContenct.Site,Url which is null.Not sure why the Context is missing. –  V.B Jul 2 '12 at 10:22
    
Thanks a lot Steve B. –  V.B Jul 2 '12 at 11:14

Feature events are asynchronous. It will execute whenever it has time to execute, you can't control that, thus it can't have a context.

If you need data about the site your feature_activated method triggered on, use

var web = properties.feature.parent as SPWeb

For a web-scoped feature

var site = properties.feature.parent as SPSite

for a site scoped feature instead of trying to use the context object.

share|improve this answer

you have to find out more details about source of this exception. it can be done by looking inside log file "...14\logs" or using "ULS Viewer", there you can find answer - which object throws exception and why?

ps. usually it hapens when you try to reference to list or group... which not exists

share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.