Dealing with Null Values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Max Sandman

    Dealing with Null Values

    I'm getting increasingly frustrated with C# and its exceptions on null
    values. Rather than try to deal with it on a hit-or-miss basis as
    exceptions pop up, I thought I should try to learn exactly how C# deals
    with null. Of course, there's nothing obvious in the docs like "Dealing
    with Null Values" and a search on "null" yielded 500 results, most of
    which don't apply. Can anybody point me in the right direction? Or offer
    some general guidelines? Or even the correct keyword to search the docs?

    sandman

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Jon Skeet

    #2
    Re: Dealing with Null Values

    Max Sandman <sandman_max@ya hoo.com> wrote:[color=blue]
    > I'm getting increasingly frustrated with C# and its exceptions on null
    > values. Rather than try to deal with it on a hit-or-miss basis as
    > exceptions pop up, I thought I should try to learn exactly how C# deals
    > with null. Of course, there's nothing obvious in the docs like "Dealing
    > with Null Values" and a search on "null" yielded 500 results, most of
    > which don't apply. Can anybody point me in the right direction? Or offer
    > some general guidelines? Or even the correct keyword to search the docs?[/color]

    Never try to dereference null - that's when you'll get an exception.

    Read the documentation for methods you call - they should (but probably
    won't always, unfortunately) specify whether or not null is a valid
    value for the parameters you pass in, and whether or not the method may
    return null.

    Basically, make sure you know at all times which of your variables may
    be null, and what it means for them to be null. Then don't dereference
    those null values :)

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Ignacio Machin

      #3
      Re: Dealing with Null Values

      Hi Max,


      Well I think that you will not find what you are looking for, now the
      question to ask is why you are getting so much null values in your program,
      are you sure you initialize all your variables correctly? are you checking
      for null before using a object, it's non uncommon method returning null when
      a value is not found like this example:

      DataRow row = dataset.Tables["table1"].Rows.Find( pk );
      //I have to check if row is a valid row or null
      if ( row != null ) { .... }


      Other than that, I do not what to advise you.

      Hope this help,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation

      "Max Sandman" <sandman_max@ya hoo.com> wrote in message
      news:uEc01TjdDH A.3088@TK2MSFTN GP09.phx.gbl...[color=blue]
      > I'm getting increasingly frustrated with C# and its exceptions on null
      > values. Rather than try to deal with it on a hit-or-miss basis as
      > exceptions pop up, I thought I should try to learn exactly how C# deals
      > with null. Of course, there's nothing obvious in the docs like "Dealing
      > with Null Values" and a search on "null" yielded 500 results, most of
      > which don't apply. Can anybody point me in the right direction? Or offer
      > some general guidelines? Or even the correct keyword to search the docs?
      >
      > sandman
      >
      > *** Sent via Developersdex http://www.developersdex.com ***
      > Don't just participate in USENET...get rewarded for it![/color]


      Comment

      • JS

        #4
        Dealing with Null Values

        with respect to Nulls...what are your frustrations?
        examples?


        [color=blue]
        >-----Original Message-----
        >I'm getting increasingly frustrated with C# and its[/color]
        exceptions on null[color=blue]
        >values. Rather than try to deal with it on a hit-or-[/color]
        miss basis as[color=blue]
        >exceptions pop up, I thought I should try to learn[/color]
        exactly how C# deals[color=blue]
        >with null. Of course, there's nothing obvious in the[/color]
        docs like "Dealing[color=blue]
        >with Null Values" and a search on "null" yielded 500[/color]
        results, most of[color=blue]
        >which don't apply. Can anybody point me in the right[/color]
        direction? Or offer[color=blue]
        >some general guidelines? Or even the correct keyword to[/color]
        search the docs?[color=blue]
        >
        >sandman
        >
        >*** Sent via Developersdex http://www.developersdex.com[/color]
        ***[color=blue]
        >Don't just participate in USENET...get rewarded for it!
        >.
        >[/color]

        Comment

        • Richard Cook

          #5
          Re: Dealing with Null Values

          there's a null object pattern too, which is sometimes appropriate to save
          explicit tests for null



          "Jon Skeet" <skeet@pobox.co m> wrote in message
          news:MPG.19c6d9 224e283fc998a4d 9@news.microsof t.com...[color=blue]
          > Max Sandman <sandman_max@ya hoo.com> wrote:[color=green]
          > > I'm getting increasingly frustrated with C# and its exceptions on null
          > > values. Rather than try to deal with it on a hit-or-miss basis as
          > > exceptions pop up, I thought I should try to learn exactly how C# deals
          > > with null. Of course, there's nothing obvious in the docs like "Dealing
          > > with Null Values" and a search on "null" yielded 500 results, most of
          > > which don't apply. Can anybody point me in the right direction? Or offer
          > > some general guidelines? Or even the correct keyword to search the docs?[/color]
          >
          > Never try to dereference null - that's when you'll get an exception.
          >
          > Read the documentation for methods you call - they should (but probably
          > won't always, unfortunately) specify whether or not null is a valid
          > value for the parameters you pass in, and whether or not the method may
          > return null.
          >
          > Basically, make sure you know at all times which of your variables may
          > be null, and what it means for them to be null. Then don't dereference
          > those null values :)
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet/
          > If replying to the group, please do not mail me too[/color]


          Comment

          • Joe

            #6
            Re: Dealing with Null Values


            "Max Sandman" <sandman_max@ya hoo.com> wrote in message
            news:uEc01TjdDH A.3088@TK2MSFTN GP09.phx.gbl...[color=blue]
            > I'm getting increasingly frustrated with C# and its exceptions on null
            > values. Rather than try to deal with it on a hit-or-miss basis as
            > exceptions pop up, I thought I should try to learn exactly how C# deals
            > with null. Of course, there's nothing obvious in the docs like "Dealing
            > with Null Values" and a search on "null" yielded 500 results, most of
            > which don't apply. Can anybody point me in the right direction? Or offer
            > some general guidelines? Or even the correct keyword to search the docs?[/color]

            Hi Max,

            Off the top of my head, here are a few coding practices that I use to avoid
            these types of problems:

            1. Initialize variables so you always know they are in a valid state. If
            you explicitly initialize them to null, you know that they start off in an
            invalid state (anticipating that the algorithm should bring them to a valid
            state) and you should check them before you use them.

            2. Check for null before making library calls to 3rd pary libraries where
            null is invalid.

            3. Use Debug.Assert for pre and post-condition checking to make sure your
            algorithms start and end in valid states. It's really easy to use for
            pre-conditions, i.e. making sure input parameters are not null.

            4. Get NUnit, http://nunit.org/, (or another unit testing tool that you
            would prefer) and test positive and negative conditions. I use this all the
            time and do regular regression tests that keep me out of trouble.

            Joe
            --
            Welcome to C# Station!  This is a community site for people interested in applying .NET using the C# programming language.  We’ve been around since July 4th 2000 and have continued to grow over the years.  Items of interest include Articles, Books, Links, Documentation,  and Tutorials. More… Source Code If you would like to see an […]



            Comment

            • Jon Skeet

              #7
              Re: Dealing with Null Values

              Joe <joe@nospam.com > wrote:
              [color=blue]
              > 3. Use Debug.Assert for pre and post-condition checking to make sure your
              > algorithms start and end in valid states. It's really easy to use for
              > pre-conditions, i.e. making sure input parameters are not null.[/color]

              I would suggest not using Debug.Assert for that kind of thing - I'd use
              a straight exception:

              if (someParameter= =null)
              throw new ArgumentNullExc eption ("someParameter ");

              At least for non-private methods - it would make sense to use
              assertions for private methods.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Joe

                #8
                Re: Dealing with Null Values


                "Jon Skeet" <skeet@pobox.co m> wrote in message
                news:MPG.19c6e5 43a670d85a98a4d c@news.microsof t.com...[color=blue]
                > Joe <joe@nospam.com > wrote:
                >[color=green]
                > > 3. Use Debug.Assert for pre and post-condition checking to make sure[/color][/color]
                your[color=blue][color=green]
                > > algorithms start and end in valid states. It's really easy to use for
                > > pre-conditions, i.e. making sure input parameters are not null.[/color]
                >
                > I would suggest not using Debug.Assert for that kind of thing - I'd use
                > a straight exception:
                >
                > if (someParameter= =null)
                > throw new ArgumentNullExc eption ("someParameter ");
                >
                > At least for non-private methods - it would make sense to use
                > assertions for private methods.[/color]

                Actually, I do both. Maybe I'm afraid someone else on the team will eat the
                exception and I want to make sure they don't overlook it. :)

                Joe
                --
                Welcome to C# Station!  This is a community site for people interested in applying .NET using the C# programming language.  We’ve been around since July 4th 2000 and have continued to grow over the years.  Items of interest include Articles, Books, Links, Documentation,  and Tutorials. More… Source Code If you would like to see an […]



                Comment

                • Guest's Avatar

                  #9
                  Re: Dealing with Null Values

                  I'm finding null to be a bit frustrating too. What am I missing?

                  Consider the following example...

                  object o = null;

                  System.String x = null;

                  o = null;

                  y y1=null;

                  if(o!=null)

                  {

                  Console.WriteLi ne(o.ToString() );

                  }





                  y is defined as follows:

                  class y

                  {

                  }



                  My frustration is that o is *never* null. I watch it in the debugger and
                  even the assignment makes it not null. Why?

                  System.String starts and null and happily stays that way.

                  What is going on?





                  Comment

                  • Jon Skeet

                    #10
                    Re: Dealing with Null Values

                    abel_lindburg@h otmail.com <abel_lindburg@ hotmail.com> wrote:[color=blue]
                    > I'm finding null to be a bit frustrating too. What am I missing?
                    >
                    > Consider the following example...
                    >
                    > object o = null;
                    > System.String x = null;
                    > o = null;
                    > y y1=null;
                    > if(o!=null)
                    > {
                    > Console.WriteLi ne(o.ToString() );
                    > }
                    >
                    > y is defined as follows:
                    >
                    > class y
                    > {
                    > }
                    >
                    > My frustration is that o is *never* null. I watch it in the debugger and
                    > even the assignment makes it not null. Why?[/color]

                    I'm sure it's just the debugger being strange - o certainly *is* null.
                    Here's a program to demonstrate that, using bits of your code:

                    using System;

                    public class Test
                    {
                    static void Main()
                    {
                    object o = null;

                    System.String x = null;

                    o = null;

                    if(o!=null)
                    {
                    Console.WriteLi ne(o.ToString() );
                    }
                    else
                    {
                    Console.WriteLi ne ("o was null");
                    }
                    }
                    }

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    • Ignacio Machin

                      #11
                      Re: Dealing with Null Values

                      Hi Abel,

                      In my machine it shows the three as null , both in the watch window and the
                      quickwatch.

                      Cheers,

                      --
                      Ignacio Machin,
                      ignacio.machin AT dot.state.fl.us
                      Florida Department Of Transportation

                      <abel_lindburg@ hotmail.com> wrote in message
                      news:eLLvhXndDH A.2320@TK2MSFTN GP12.phx.gbl...[color=blue]
                      > I'm finding null to be a bit frustrating too. What am I missing?
                      >
                      > Consider the following example...
                      >
                      > object o = null;
                      >
                      > System.String x = null;
                      >
                      > o = null;
                      >
                      > y y1=null;
                      >
                      > if(o!=null)
                      >
                      > {
                      >
                      > Console.WriteLi ne(o.ToString() );
                      >
                      > }
                      >
                      >
                      >
                      >
                      >
                      > y is defined as follows:
                      >
                      > class y
                      >
                      > {
                      >
                      > }
                      >
                      >
                      >
                      > My frustration is that o is *never* null. I watch it in the debugger and
                      > even the assignment makes it not null. Why?
                      >
                      > System.String starts and null and happily stays that way.
                      >
                      > What is going on?
                      >
                      >
                      >
                      >
                      >[/color]


                      Comment

                      • Max Sandman

                        #12
                        Re: Dealing with Null Values

                        Thanks everybody. Lots of good suggestions. I've got NUnit and I'll
                        start looking at it.

                        Most of my problems come from one of the following: uninitialized values
                        that, theoretically, a properly functioning app would prevent from
                        occurring; reading from a database where all columns haven't had data
                        entered - I'm still a little concerned about that one. If the user
                        doesn't enter anything, then the column contains null but if the user
                        clears a value and saves it, it's usually something defined, like 0 or
                        "". Do I just need to make sure that my database is initialized to valid
                        values? Since data will either be imported or keyed in directly, I have
                        control over how it goes in.

                        sandman

                        *** Sent via Developersdex http://www.developersdex.com ***
                        Don't just participate in USENET...get rewarded for it!

                        Comment

                        • Guest's Avatar

                          #13
                          Re: Dealing with Null Values

                          Interesting. Then I guess the question is which version of the framework are
                          you using?

                          The String is shown as null for me, while as you can see from a previous
                          posting, others see <undefined value>


                          "Ignacio Machin" <ignacio.mach in AT dot.state.fl.us > wrote in message
                          news:%23tuSyDtd DHA.1620@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                          > Hi Abel,
                          >
                          > In my machine it shows the three as null , both in the watch window and[/color]
                          the[color=blue]
                          > quickwatch.
                          >
                          > Cheers,
                          >
                          > --
                          > Ignacio Machin,
                          > ignacio.machin AT dot.state.fl.us
                          > Florida Department Of Transportation
                          >
                          > <abel_lindburg@ hotmail.com> wrote in message
                          > news:eLLvhXndDH A.2320@TK2MSFTN GP12.phx.gbl...[color=green]
                          > > I'm finding null to be a bit frustrating too. What am I missing?
                          > >
                          > > Consider the following example...
                          > >
                          > > object o = null;
                          > >
                          > > System.String x = null;
                          > >
                          > > o = null;
                          > >
                          > > y y1=null;
                          > >
                          > > if(o!=null)
                          > >
                          > > {
                          > >
                          > > Console.WriteLi ne(o.ToString() );
                          > >
                          > > }
                          > >
                          > >
                          > >
                          > >
                          > >
                          > > y is defined as follows:
                          > >
                          > > class y
                          > >
                          > > {
                          > >
                          > > }
                          > >
                          > >
                          > >
                          > > My frustration is that o is *never* null. I watch it in the debugger and
                          > > even the assignment makes it not null. Why?
                          > >
                          > > System.String starts and null and happily stays that way.
                          > >
                          > > What is going on?
                          > >
                          > >
                          > >
                          > >
                          > >[/color]
                          >
                          >[/color]


                          Comment

                          • Guest's Avatar

                            #14
                            Re: Dealing with Null Values

                            You're right, the value *is* null, since looking at the registers in the
                            disassembly confirmed this. I'm just confused as to how the debugger figures
                            to display <undefined value>. I'm using 1.1 of the Framework and it seems my
                            VS is up to date.


                            "Jon Skeet" <skeet@pobox.co m> wrote in message
                            news:MPG.19c778 c638ac89cf98a4d e@news.microsof t.com...[color=blue]
                            > abel_lindburg@h otmail.com <abel_lindburg@ hotmail.com> wrote:[color=green]
                            > > I'm finding null to be a bit frustrating too. What am I missing?
                            > >
                            > > Consider the following example...
                            > >
                            > > object o = null;
                            > > System.String x = null;
                            > > o = null;
                            > > y y1=null;
                            > > if(o!=null)
                            > > {
                            > > Console.WriteLi ne(o.ToString() );
                            > > }
                            > >
                            > > y is defined as follows:
                            > >
                            > > class y
                            > > {
                            > > }
                            > >
                            > > My frustration is that o is *never* null. I watch it in the debugger and
                            > > even the assignment makes it not null. Why?[/color]
                            >
                            > I'm sure it's just the debugger being strange - o certainly *is* null.
                            > Here's a program to demonstrate that, using bits of your code:
                            >
                            > using System;
                            >
                            > public class Test
                            > {
                            > static void Main()
                            > {
                            > object o = null;
                            >
                            > System.String x = null;
                            >
                            > o = null;
                            >
                            > if(o!=null)
                            > {
                            > Console.WriteLi ne(o.ToString() );
                            > }
                            > else
                            > {
                            > Console.WriteLi ne ("o was null");
                            > }
                            > }
                            > }
                            >
                            > --
                            > Jon Skeet - <skeet@pobox.co m>
                            > http://www.pobox.com/~skeet/
                            > If replying to the group, please do not mail me too[/color]


                            Comment

                            • Ignacio Machin

                              #15
                              Re: Dealing with Null Values

                              HI Abel,

                              I'm using VS.2002 and v1.0.3705

                              Cheers,

                              --
                              Ignacio Machin,
                              ignacio.machin AT dot.state.fl.us
                              Florida Department Of Transportation

                              <abel_lindburg@ hotmail.com> wrote in message
                              news:OI0MIbtdDH A.2296@TK2MSFTN GP09.phx.gbl...[color=blue]
                              > Interesting. Then I guess the question is which version of the framework[/color]
                              are[color=blue]
                              > you using?
                              >
                              > The String is shown as null for me, while as you can see from a previous
                              > posting, others see <undefined value>
                              >
                              >
                              > "Ignacio Machin" <ignacio.mach in AT dot.state.fl.us > wrote in message
                              > news:%23tuSyDtd DHA.1620@TK2MSF TNGP12.phx.gbl. ..[color=green]
                              > > Hi Abel,
                              > >
                              > > In my machine it shows the three as null , both in the watch window and[/color]
                              > the[color=green]
                              > > quickwatch.
                              > >
                              > > Cheers,
                              > >
                              > > --
                              > > Ignacio Machin,
                              > > ignacio.machin AT dot.state.fl.us
                              > > Florida Department Of Transportation
                              > >
                              > > <abel_lindburg@ hotmail.com> wrote in message
                              > > news:eLLvhXndDH A.2320@TK2MSFTN GP12.phx.gbl...[color=darkred]
                              > > > I'm finding null to be a bit frustrating too. What am I missing?
                              > > >
                              > > > Consider the following example...
                              > > >
                              > > > object o = null;
                              > > >
                              > > > System.String x = null;
                              > > >
                              > > > o = null;
                              > > >
                              > > > y y1=null;
                              > > >
                              > > > if(o!=null)
                              > > >
                              > > > {
                              > > >
                              > > > Console.WriteLi ne(o.ToString() );
                              > > >
                              > > > }
                              > > >
                              > > >
                              > > >
                              > > >
                              > > >
                              > > > y is defined as follows:
                              > > >
                              > > > class y
                              > > >
                              > > > {
                              > > >
                              > > > }
                              > > >
                              > > >
                              > > >
                              > > > My frustration is that o is *never* null. I watch it in the debugger[/color][/color][/color]
                              and[color=blue][color=green][color=darkred]
                              > > > even the assignment makes it not null. Why?
                              > > >
                              > > > System.String starts and null and happily stays that way.
                              > > >
                              > > > What is going on?
                              > > >
                              > > >
                              > > >
                              > > >
                              > > >[/color]
                              > >
                              > >[/color]
                              >
                              >[/color]


                              Comment

                              Working...