Getting NullReferenceException instead of ArgumentNullException
-
The .Net documentation for
String.Contains()
(http://msdn2.microsoft.com/en-us/library/system.string.contains.aspx) says it may raise theSystem.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? -
The .Net documentation for
String.Contains()
(http://msdn2.microsoft.com/en-us/library/system.string.contains.aspx) says it may raise theSystem.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? -
Have you checked
myString
to see if it'snull
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.
-
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?
It's not a
string
exception, it's a runtime exception. You're calling a method on anull
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.
-
It's not a
string
exception, it's a runtime exception. You're calling a method on anull
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.
-
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...
-
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!
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.