I cretaed a Visual web part in SP 2013. It needs a SharePoint list. So wrote below code in web part feature receiver. When I deploy the solution I get error says Error occurred in deployment step 'Activate Features': Object reference not set to an instance of an object. I set ActivateOnDefault="FALSE" and AlwaysForceInstall="TRUE" . Take a look on my feature manifest file.
<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Title="SharePointProject1 Feature1" ActivateOnDefault="FALSE" AlwaysForceInstall="TRUE" Id="8f7163d5-6c65-40d8-9045-8f74192f07d7" ReceiverAssembly="SharePointProject1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9c2f0c3a8e22f6a0" ReceiverClass="SharePointProject1.Features.Feature1.Feature1EventReceiver" Scope="Site">
<ElementManifests>
<ElementManifest Location="VisualWebPart1\Elements.xml" />
<ElementFile Location="VisualWebPart1\VisualWebPart1.webpart" />
</ElementManifests>
</Feature>
This is my Feature1EventReceiver code.
SPWeb spWeb = properties.Feature.Parent as SPWeb;
SPList laptopList = spWeb.Lists["Laptops"];
if (laptopList != null)
{
laptopList.Delete();
}
SPListCollection lists = spWeb.Lists;
lists.Add("Laptops", "The Laptops", SPListTemplateType.GenericList);
laptopList = spWeb.Lists["Laptops"];
laptopList.Fields.Add("Name", SPFieldType.Text, true);
laptopList.Fields.Add("Model", SPFieldType.Text, true);
laptopList.Fields.Add("Image", SPFieldType.URL, false);
I can deploy if I have only below code
SPWeb spWeb = properties.Feature.Parent as SPWeb;