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

相關問題