This page was last reviewed on Jun 21, 2021.
Dot Net Perls
NullReferenceException. This error indicates that you are trying to access member fields, or function types, on an object reference that points to null.
C# error notes. An exception indicates a flaw in the code, or an invalid configuration or missing file. Often a code change is needed to correct the C# program.
First example. This program causes a NullReferenceException to be raised. The program explicitly assigns the string reference variable to the null literal.
Note This means that the string variable does not point to any object on the managed heap. It is equivalent to a null pointer.
Detail The string reference variable is assigned to the null literal. Next, the Length property is accessed on the string reference.
Info The program always throws an execution. You cannot access an instance property like Length on a null reference.
using System; class Program { static void Main() { string value = null; if (value.Length == 0) // <-- Causes exception { Console.WriteLine(value); // <-- Never reached } } }
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at Program.Main() in C:\Users\...
Example 2. We can prevent a NullReferenceException. If the parameter points to null, the compiler will not know this at compile-time. You must check for null at the method start.
Info An array containing 2 elements is passed to the Test method. The Test method checks its parameter against the null literal.
Detail Test() will not throw when passed a null reference, as we see in the latter part of the Main method.
using System; class Program { static void Main() { // Create an array and use it in a method. int[] array = new int[2]; array[0] = 1; array[1] = 2; Test(array); // Use null reference in a method. array = null; Test(array); // <-- Won't crash } static void Test(int[] array) { if (array == null) { // You can throw an exception here, or deal with the argument. return; } int rank = array.Rank; Console.WriteLine(rank); } }
Discussion. Exception handling is a complex topic. A common theme to the best approaches is to always log the exceptions and work to prevent them from happening in normal runtime operation.
Also In many programs you can use ArgumentNullException to notify the caller of errors.
Handling null references. If you have total control over the callers of a method and it can never be called with a null parameter, it is preferable not to check the parameter for null.
But For libraries that will be used by others, more careful parameter checking to avoid NullReferenceExceptions is best.
Summary. We looked the NullReferenceException error reporting class. You will encounter this exception when you attempt to access a member on a null variable.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 21, 2021 (edit).
© 2007-2024 Sam Allen.