Question: Object Reference not set to an instance of an object.

Discussion in 'ASP .Net' started by =?Utf-8?B?TWFubnkgQ2hvaGFu?=, Jan 3, 2005.

  1. Hi Guys,

    been puzzled with this error for few days now. In the try and catch block, i
    am trying to set the label text property to property retrieved from exchange
    database. The record has certain properties set and some are not. I keep on
    getting error Object reference not set to an instance of an object...

    here is my code
    if(!indvContact.Surname.Equals(null))
    {
    surname.Text=indvContact.Surname.ToString();
    }
    I assumed that it will work however it dont. Is there any way in the catch
    block, i can tell the code to move on to the next step ...or maybe another
    approach to it..

    Thanks
     
    =?Utf-8?B?TWFubnkgQ2hvaGFu?=, Jan 3, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?TWFubnkgQ2hvaGFu?=

    Marina Guest

    I think you should first learn what it means for an object to be null.

    if the Surname property is null - then accessing its Equals method is going
    to fail - because the property is null! And 'null' does not have an Equals
    method. So in cases where Surname is null, this call fails.

    I am not sure why it would even occur to you to check for null this way?
    This should be done:

    if (!indvContact.Surname == null)
    {
    }

    "Manny Chohan" <> wrote in message
    news:...
    > Hi Guys,
    >
    > been puzzled with this error for few days now. In the try and catch block,

    i
    > am trying to set the label text property to property retrieved from

    exchange
    > database. The record has certain properties set and some are not. I keep

    on
    > getting error Object reference not set to an instance of an object...
    >
    > here is my code
    > if(!indvContact.Surname.Equals(null))
    > {
    > surname.Text=indvContact.Surname.ToString();
    > }
    > I assumed that it will work however it dont. Is there any way in the catch
    > block, i can tell the code to move on to the next step ...or maybe another
    > approach to it..
    >
    > Thanks
     
    Marina, Jan 3, 2005
    #2
    1. Advertising

  3. Re: Question: Object Reference not set to an instance of an object

    Thanks Mariana, i will try this.

    "Marina" wrote:

    > I think you should first learn what it means for an object to be null.
    >
    > if the Surname property is null - then accessing its Equals method is going
    > to fail - because the property is null! And 'null' does not have an Equals
    > method. So in cases where Surname is null, this call fails.
    >
    > I am not sure why it would even occur to you to check for null this way?
    > This should be done:
    >
    > if (!indvContact.Surname == null)
    > {
    > }
    >
    > "Manny Chohan" <> wrote in message
    > news:...
    > > Hi Guys,
    > >
    > > been puzzled with this error for few days now. In the try and catch block,

    > i
    > > am trying to set the label text property to property retrieved from

    > exchange
    > > database. The record has certain properties set and some are not. I keep

    > on
    > > getting error Object reference not set to an instance of an object...
    > >
    > > here is my code
    > > if(!indvContact.Surname.Equals(null))
    > > {
    > > surname.Text=indvContact.Surname.ToString();
    > > }
    > > I assumed that it will work however it dont. Is there any way in the catch
    > > block, i can tell the code to move on to the next step ...or maybe another
    > > approach to it..
    > >
    > > Thanks

    >
    >
    >
     
    =?Utf-8?B?TWFubnkgQ2hvaGFu?=, Jan 3, 2005
    #3
  4. Re: Question: Object Reference not set to an instance of an object

    if (indvContact.Surname != null) //is easier to read.
     
    =?Utf-8?B?U2NvdHQgU2ltb25z?=, Jan 3, 2005
    #4
  5. Actually, there are two possibilities of null objects in the code, either of
    which would throw the same error:

    >> if(!indvContact.Surname.Equals(null))
    >> {
    >> surname.Text=indvContact.Surname.ToString();
    >> }


    Object References:
    indvContact
    surname

    indvContact.Surname is also an Object Reference, but it is used in the first
    part to see whether it is null, which would not throw an exception, unless
    indvContact itself is null.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Neither a follower nor a lender be.

    "Marina" <> wrote in message
    news:...
    >I think you should first learn what it means for an object to be null.
    >
    > if the Surname property is null - then accessing its Equals method is
    > going
    > to fail - because the property is null! And 'null' does not have an
    > Equals
    > method. So in cases where Surname is null, this call fails.
    >
    > I am not sure why it would even occur to you to check for null this way?
    > This should be done:
    >
    > if (!indvContact.Surname == null)
    > {
    > }
    >
    > "Manny Chohan" <> wrote in message
    > news:...
    >> Hi Guys,
    >>
    >> been puzzled with this error for few days now. In the try and catch
    >> block,

    > i
    >> am trying to set the label text property to property retrieved from

    > exchange
    >> database. The record has certain properties set and some are not. I keep

    > on
    >> getting error Object reference not set to an instance of an object...
    >>
    >> here is my code
    >> if(!indvContact.Surname.Equals(null))
    >> {
    >> surname.Text=indvContact.Surname.ToString();
    >> }
    >> I assumed that it will work however it dont. Is there any way in the
    >> catch
    >> block, i can tell the code to move on to the next step ...or maybe
    >> another
    >> approach to it..
    >>
    >> Thanks

    >
    >
     
    Kevin Spencer, Jan 3, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Parthiv Joshi
    Replies:
    2
    Views:
    691
    Kalyan
    Jul 2, 2004
  2. Suresh Kojhani
    Replies:
    1
    Views:
    2,384
    Anushi
    Jul 29, 2004
  3. Chris Fink
    Replies:
    2
    Views:
    4,091
    David Waz...
    Jul 3, 2003
  4. yysiow
    Replies:
    1
    Views:
    433
    Kevin Spencer
    Jul 12, 2003
  5. Replies:
    1
    Views:
    481
    Mark Rae [MVP]
    Sep 20, 2007
Loading...

Share This Page