Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Getting NullReferenceException instead of ArgumentNullException

Getting NullReferenceException instead of ArgumentNullException

Scheduled Pinned Locked Moved C#
csharpcomquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    The .Net documentation for String.Contains() (http://msdn2.microsoft.com/en-us/library/system.string.contains.aspx) says it may raise the System.ArgumentNullException exception. This is the only exception listed for this method. However, when I use this method to examine the contents of string based object properties, I'm getting the following exception instead: System.NullReferenceException: Object reference not set to an instance of an object Note that the exception doesn't happen unless I use the .Contains() method. For instance, in the following code: string myString = object.stringProperty; MessageBox.Show(myString); if (myString.Contains("a")) { ... } no exception is thrown until the "if" statement is executed. So, is the documentation incorrect, or is there something special about the empty string being held by the object property?

    M 1 Reply Last reply
    0
    • J JoeRip

      The .Net documentation for String.Contains() (http://msdn2.microsoft.com/en-us/library/system.string.contains.aspx) says it may raise the System.ArgumentNullException exception. This is the only exception listed for this method. However, when I use this method to examine the contents of string based object properties, I'm getting the following exception instead: System.NullReferenceException: Object reference not set to an instance of an object Note that the exception doesn't happen unless I use the .Contains() method. For instance, in the following code: string myString = object.stringProperty; MessageBox.Show(myString); if (myString.Contains("a")) { ... } no exception is thrown until the "if" statement is executed. So, is the documentation incorrect, or is there something special about the empty string being held by the object property?

      M Offline
      M Offline
      mid 5741
      wrote on last edited by
      #2

      Have you checked myString to see if it's null or not?

      Cyril Connolly wrote:

      Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.

      J 1 Reply Last reply
      0
      • M mid 5741

        Have you checked myString to see if it's null or not?

        Cyril Connolly wrote:

        Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.

        J Offline
        J Offline
        JoeRip
        wrote on last edited by
        #3

        Clearly, I have not :-). But I'm not sure what point you are making? Am I wrong in assuming that the list of Exceptions in the String.Contains() documentation is exclusive and complete?

        M 1 Reply Last reply
        0
        • J JoeRip

          Clearly, I have not :-). But I'm not sure what point you are making? Am I wrong in assuming that the list of Exceptions in the String.Contains() documentation is exclusive and complete?

          M Offline
          M Offline
          mid 5741
          wrote on last edited by
          #4

          It's not a string exception, it's a runtime exception. You're calling a method on a null object.

          Cyril Connolly wrote:

          Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.

          J 1 Reply Last reply
          0
          • M mid 5741

            It's not a string exception, it's a runtime exception. You're calling a method on a null object.

            Cyril Connolly wrote:

            Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.

            J Offline
            J Offline
            JoeRip
            wrote on last edited by
            #5

            I guess I'm confused why none of the other code is throwing an exception on the null object. I can pass that object to MessageBox.Show, I can add another string to it, etc. It's only when I call .Contains() that it coughs up a lung...

            J 1 Reply Last reply
            0
            • J JoeRip

              I guess I'm confused why none of the other code is throwing an exception on the null object. I can pass that object to MessageBox.Show, I can add another string to it, etc. It's only when I call .Contains() that it coughs up a lung...

              J Offline
              J Offline
              JoeRip
              wrote on last edited by
              #6

              However, thanks for your help. I'll guess I'll have to be more defensive in null checking on reference values returned from properties. Dang those inconsiderate objects!

              M 1 Reply Last reply
              0
              • J JoeRip

                However, thanks for your help. I'll guess I'll have to be more defensive in null checking on reference values returned from properties. Dang those inconsiderate objects!

                M Offline
                M Offline
                mid 5741
                wrote on last edited by
                #7

                Are you aware of FxCop[^]? I don't know if it will identify the problem above, but I analyzed a project and FxCop found a couple of instances of not checking if an object was valid before using it.

                Cyril Connolly wrote:

                Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups