开发者

System.NullReferenceException: Object reference not set to an instance of an object [duplicate]

开发者 https://www.devze.com 2023-03-09 19:07 出处:网络
Thi开发者_开发知识库s question already has answers here: Closed 11 years ago. Possible Duplicates:
Thi开发者_开发知识库s question already has answers here: Closed 11 years ago.

Possible Duplicates: What is a NullReferenceException in .NET? System.NullReferenceException: Object reference not set to an instance of an object

I am using the following code.

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)  {

So here on this code I am getting the error

this._section.GetType();

How can this problem be fixed?


I think you forget to set value for _section. You should have set it, for example, in updateViews.

I believe that you plan _section to be an instance of some subclass of SingleBanners, which to be determined at runtime. If the type of _section is clear at compile-time (like _section = new SingleBanners()), you would have used typeof(SingleBanners).


You can't execute a non-static method on a object that wasn't instantiated.

Try this:

private SingleBanners _section = new SingleBanners();


Most likely, it means that _section is null and has not been set. You either need

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

or

_section = ...

somewhere else before you can use it.


The answer is in the error Object Reference not set to an Instance of an Object...

You declared the object _section, but you have not set a reference to it?

Something like:

private SingleBanners _section = new SingleBanners();

The way you've declared the private SingleBanners _section; the reference to the object _section will be null!


Instead of trying

_section.GetType()

use

typeof(SingleBanners )
0

精彩评论

暂无评论...
验证码 换一张
取 消