0

的实例,我有以下激活码:“激活功能”:对象引用未设置到对象

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(); 
     } 
    } 

哪些应该通过并添加新的列表称为项目及其相关视图。如何过上运行的应用程序时,我得到:

“激活功能”:对象引用不设置到 对象的实例

我的问题是:

  • 这是为什么发生了什么?激活发生在站点级别。我是“开发”网站的“admin”。
  • 我应该每次检查以确保此列表不存在? (每次,指每次我打部署)
+0

哪条线抛出异常?这应该是绝对的第一件事,你应该把它包括在问题中。 –

+0

它实际上并没有说哪一行,它是0行,0行,0行,CustomerCommunicationProject。 @jonSkeet – TheWebs

+0

您没有完整的堆栈跟踪?伊克。你能通过调试器中的代码吗? (如果你当前正在运行Release版本,请试试Debug版本,这可能会给你更多的信息。) –

回答

1

我会假设你有一个网站范围的功能,并且您的NullReferenceException是由你试图施放properties.Feature.Parent as SPWeb造成的。

如果我关于您的特征为Site的范围的假设是正确的,那么您无法以您尝试的方式访问SPWeb。试试这个:

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. 
    } 
} 
+0

我会继续使用我的使用声明吗?林不知道这是否改变。 – TheWebs

+0

@TheWebs - 不需要处理根网站,只需处理网站(我认为)。 – RobH

相关问题