2011-06-02 236 views
-2

可能顯示的文件: What is a NullReferenceException in .NET? System.NullReferenceException: Object reference not set to an instance of an objectSystem.NullReferenceException:對象未設置爲一個對象的一個​​實例

我使用下面的代碼。

public partial class SectionControls_SingleBanners : SectionControlBaseClass 
{ 
    private SingleBanners _section; 

    protected void Page_PreRender(object sender, EventArgs e) { 
     updateViews(); 

     if (RssCapable(this._section.GetType()) && _section.BannersEntries.Rows.Count > 0) { 

所以這裏這個代碼我收到錯誤

this._section.GetType(); 

怎麼能這個問題能解決嗎?

+0

上述錯誤現在已經解決了我面對這裏同樣的問題....保護無效btnSaveDetails_Click(對象發件人,EventArgs的){ 的DataRow row = null;如果(ViewState [「編輯」]。ToString()==「新」){ – shafiq 2011-06-02 10:09:45

回答

0

最有可能的是,它表示_section爲空並且尚未設置。您也需要

private SingleBanners _section = new SingleBanners(...); 

_section = ... 

別的地方,然後才能使用它。

1

對於未實例化的對象,您無法執行非靜態方法。

試試這個:

private SingleBanners _section = new SingleBanners(); 
+0

噢非常感謝這很適合我 – shafiq 2011-06-02 10:03:17

+0

@shafiq:如果你喜歡答案,+1,如果你認爲它是最好的一,檢查它作爲接受的答案 – Sergio 2011-06-02 10:10:44

0

答案是錯誤Object Reference not set to an Instance of an Object...

你宣佈object _section,但您沒有設置它的參考?

喜歡的東西:

private SingleBanners _section = new SingleBanners(); 

聲明瞭私人SingleBanners _section的方式;對object _section的引用將爲空!

1

我想你忘了設置_section的值。例如,您應該將其設置爲updateViews

我相信你計劃_section是一個SingleBanners的一些子類的實例,它將在運行時確定。如果編譯時清除_section的類型(如_section = new SingleBanners()),則應使用typeof(SingleBanners)。

0

,而不是試圖

_section.GetType() 

使用

typeof(SingleBanners) 
+0

現在這段代碼給我同樣的錯誤....保護無效btnSaveDetails_Click(對象發件人,EventArgs e){0} DataRow row = null; (ViewState [「Edit」]。ToString()==「new」){ – shafiq 2011-06-02 10:06:10

+0

什麼是VewSate [「Edit」]?在哪裏分配?需要分享一些更多的代碼來找出錯誤發生的原因。 這裏是因爲VewSate [「Edit」]爲空 – 2011-06-02 11:04:41

相關問題