0

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?

Community
  • 1
  • 1
shafiq
  • 45
  • 1
  • 2
  • 3
  • the above error resolved now i am facing the same issue here....protected void btnSaveDetails_Click(object sender, EventArgs e) { DataRow row = null; if (ViewState["Edit"].ToString() == "new") { – shafiq Jun 02 '11 at 10:09

5 Answers5

1

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

Try this:

private SingleBanners _section = new SingleBanners();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sergio
  • 8,125
  • 10
  • 46
  • 77
1

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

Truong Hong Thi
  • 374
  • 1
  • 6
0

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.

tofutim
  • 22,664
  • 20
  • 87
  • 148
0

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dave Long
  • 794
  • 4
  • 7
0

Instead of trying

_section.GetType()

use

typeof(SingleBanners )
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Umesh CHILAKA
  • 1,466
  • 14
  • 25
  • now this code give me the same error....protected void btnSaveDetails_Click(object sender, EventArgs e) { DataRow row = null; if (ViewState["Edit"].ToString() == "new") { – shafiq Jun 02 '11 at 10:06
  • what is VewSate["Edit"]? where it is assigned? need to share some more code to find out why that error is coming. here it is because VewSate["Edit"] is null – Umesh CHILAKA Jun 02 '11 at 11:04