0

I have the following activation code:

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        // Create a new list and populate it.
        using (SPWeb web = properties.Feature.Parent as SPWeb)
        {
            web.Lists.Add("Projects", "Projects That are currently being worked on.", SPListTemplateType.GenericList);
            web.Update();

            // Add the new list and the new content.
            SPList projectList = web.Lists["Projects"];
            projectList.Fields.Add("Name", SPFieldType.Text, false);
            projectList.Fields.Add("Description", SPFieldType.Text, false);
            projectList.Update();

            //Create the view? - Possibly remove me.
            System.Collections.Specialized.StringCollection stringCollection = 
                new System.Collections.Specialized.StringCollection();
            stringCollection.Add("Name");
            stringCollection.Add("Description");

            //Add the list.
            projectList.Views.Add("Project Summary", stringCollection, @"", 100, 
                true, true, Microsoft.SharePoint.SPViewCollection.SPViewType.Html, false);
            projectList.Update();
        }
    }

Which should go through and add a new list called project and its associated view. How ever when running the app I get:

'Activate Features': Object reference not set to an instance of an object

My questions are:

  • Why is this happening? The activation happens at a site level. and I am the admin of the "development" site.
  • Should I be checking each time to make sure this list doesn't already exist? (each time, referring to each time I hit deploy)
TheWebs
  • 12,470
  • 30
  • 107
  • 211
  • Which line is throwing the exception? That should be absolute first thing you look at, and it should be included in the question. – Jon Skeet Jun 04 '13 at 14:37
  • It actually doesn't say which line, It sais Line 0, Collumn 0, CustomerCommunicationProject. @jonSkeet – TheWebs Jun 04 '13 at 14:38
  • You don't have a full stack trace? Ick. Can you step through the code in a debugger? (If you're currently running the Release build, try the Debug build instead, which may well give you more information.) – Jon Skeet Jun 04 '13 at 14:39
  • Actually looking into that >.> How ever thats a whole other issue, apparently the debugger is not properly installed and thus I cant debug this type of code. One issue after another >.> – TheWebs Jun 04 '13 at 14:40
  • Well that's the first thing to fix. Trying to develop without a decent environment is not going to be fun. Sort that out, then come back to this issue. – Jon Skeet Jun 04 '13 at 14:42
  • I just work with what Im given – TheWebs Jun 04 '13 at 14:42
  • Change this line `properties.Feature.Parent as SPWeb` to `(SPWeb)properties.Feature.Parent` - does that give you an invalid cast exception instead? Life would definitely be easier if you fixed your environment first. – RobH Jun 04 '13 at 15:07

1 Answers1

1

I'm going to assume that you have a Site scoped feature and that your NullReferenceException is being caused by you attempting to cast properties.Feature.Parent as SPWeb.

If my assumption about your feature being Site scoped is correct you can't get access to an SPWeb the way you are trying. Try this instead:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPSite siteCollection = properties.Feature.Parent as SPSite;
    if (siteCollection != null) 
    {
        SPWeb web = siteCollection.RootWeb;
        // Rest of your code here.
    }
}
RobH
  • 3,604
  • 1
  • 23
  • 46