2011-06-02 121 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

相关问题