Handling Object Null Reference Errors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • robin9876@hotmail.com

    #1

    Handling Object Null Reference Errors

    Instead of .Net showing an Object reference not set to an instance of
    an object error message. Is their a method of handling these messages
    and displaying a more user friendly message and recording this
    information for further investigation?

  • Mark Rae

    #2
    Re: Handling Object Null Reference Errors

    <robin9876@hotm ail.comwrote in message
    news:1174581465 .515401.276260@ y80g2000hsf.goo glegroups.com.. .
    Instead of .Net showing an Object reference not set to an instance of
    an object error message. Is their a method of handling these messages
    and displaying a more user friendly message and recording this
    information for further investigation?
    if (MyObject == null)
    {
    // do something
    }


    Comment

    • Aidy

      #3
      Re: Handling Object Null Reference Errors

      You could also look into custom error pages to catch all your unexpected
      errors and log them to a database while showing a friendly message.

      However you should also do what Mark alluded to, and try and write more
      robust code. Never assume a method is going to return an object, always
      check that it does.

      <robin9876@hotm ail.comwrote in message
      news:1174581465 .515401.276260@ y80g2000hsf.goo glegroups.com.. .
      Instead of .Net showing an Object reference not set to an instance of
      an object error message. Is their a method of handling these messages
      and displaying a more user friendly message and recording this
      information for further investigation?
      >

      Comment

      • Patrice

        #4
        Re: Handling Object Null Reference Errors

        See : http://support.microsoft.com/kb/306355/en-us
        (HOW TO: Create Custom Error Reporting Pages in ASP.NET by Using Visual C#
        ..NET), applies as well to VB.NET...
        ---
        Patrice

        <robin9876@hotm ail.coma écrit dans le message de news:
        1174581465.5154 01.276260@y80g2 00...legr oups.com...
        Instead of .Net showing an Object reference not set to an instance of
        an object error message. Is their a method of handling these messages
        and displaying a more user friendly message and recording this
        information for further investigation?
        >

        Comment

        • sloan

          #5
          Re: Handling Object Null Reference Errors


          Not really.

          As others have already said:

          Write better/more robust code.


          Employee e = Something.GetEm ployee();
          if(null!=e)
          {
          //do something with e
          }

          OR

          Employee e = Something.GetEm ployee();
          if(null==e)
          {
          throw new ArgumentExcepti on ("Employee was expected to be found, but was
          not");
          }

          ArgumentExcepti on can/should be replaced with your own custom exception,
          like
          EmployeeNotFoun dException : ApplicationExce ption

          ...

          The better you code, the better your debugging time and maintenance efforts
          will be.

          ...

          I just had this happen at work.
          I had coded up some xpath/xml code.

          I was told "yeah, the xyz attribute will always be there".

          I coded to search for the attribute (and its value).
          However, I coded it to throw an exception if this attribute was NOT found.

          2 days later.
          "Your code is blowing up"
          "Oh really?"

          I check the code, and its working perfectly.
          I was like "You said it would always have the xyz attribute".
          "It does have the xyz attribute.

          We check the Xml.

          He had
          Xyz in the xml.
          Xml is case sensitive.

          "Oh , I didn't know xml was case sensitive".

          So it was a quick fix. Mainly because I had anticipated the situation, and
          wrote the code to handle (or throw an exception) if something was wrong.

          It didn't take hours of debugging because I got some "object was null"
          exception.

          Better code always means faster debugging and maintenance times.





          <robin9876@hotm ail.comwrote in message
          news:1174581465 .515401.276260@ y80g2000hsf.goo glegroups.com.. .
          Instead of .Net showing an Object reference not set to an instance of
          an object error message. Is their a method of handling these messages
          and displaying a more user friendly message and recording this
          information for further investigation?
          >

          Comment

          Working...