From a2f2a1d6439b04f2fb86bf43e2d2ab21b64754ea Mon Sep 17 00:00:00 2001 From: Arvind Ganesh Date: Tue, 26 Dec 2023 03:20:45 +0530 Subject: [PATCH 1/3] NullReferenceUpdate: Added scenario where type-independent list need not be initialized --- .../Overview/example3.cs | 33 +++++++++++++++++++ xml/System/NullReferenceException.xml | 6 ++++ 2 files changed, 39 insertions(+) create mode 100644 snippets/csharp/System/NullReferenceException/Overview/example3.cs diff --git a/snippets/csharp/System/NullReferenceException/Overview/example3.cs b/snippets/csharp/System/NullReferenceException/Overview/example3.cs new file mode 100644 index 00000000000..a070ce1a433 --- /dev/null +++ b/snippets/csharp/System/NullReferenceException/Overview/example3.cs @@ -0,0 +1,33 @@ +// +using System; +using System.Collections.Generic; +using System.Collections; +using System.Runtime.Serialization; + +public class NullReferenceExample +{ + public static void Main() + { + var listType = GetListType(); + var listObj = GetList(listType); + } + + private static Type GetListType() + { + return typeof(List); + } + + private static IList GetList(Type type) + { + var emptyList = (IList)FormatterServices.GetUninitializedObject(type); // Does not call list constructor + var value = 1; + emptyList.Add(value); + return emptyList; + } +} +// The example displays output like the following: +// Unhandled Exception: System.NullReferenceException: 'Object reference +// not set to an instance of an object.' +// at System.Collections.Generic.List`1.System.Collections.IList.Add(Object item) +// at Example.GetList(Type type): line 24 +// diff --git a/xml/System/NullReferenceException.xml b/xml/System/NullReferenceException.xml index 2305d4c30bc..1334a2751bb 100644 --- a/xml/System/NullReferenceException.xml +++ b/xml/System/NullReferenceException.xml @@ -150,6 +150,12 @@ To address this issue, make sure that the argument passed to the method is not `null`, or handle the thrown exception in a `try…catch…finally` block. For more information, see [Exceptions](/dotnet/standard/exceptions/). +- A exception can be thrown when a list is created without knowing the type, but the list was not initialized. The `GetList` method in the following example throws the exception at the line `emptyList.Add(value)`. + + :::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/example3.cs" id="Snippet12"::: + + To address this issue, make sure that the list is initialized (one way to do this is to call `Activator.CreateInstance` method instead of `FormatterServices.GetUninitializedObject`), or handle the thrown exception in a `try…catch…finally` block. For more information, see [Exceptions](/dotnet/standard/exceptions/). + The following Microsoft intermediate language (MSIL) instructions throw : `callvirt`, `cpblk`, `cpobj`, `initblk`, `ldelem.`, `ldelema`, `ldfld`, `ldflda`, `ldind.`, `ldlen`, `stelem.`, `stfld`, `stind.`, `throw`, and `unbox`. uses the HRESULT COR_E_NULLREFERENCE, which has the value 0x80004003. From 33e38c0886b2098899149019567e271689b8eb8b Mon Sep 17 00:00:00 2001 From: Arvind Ganesh Date: Fri, 8 Mar 2024 11:58:20 +0530 Subject: [PATCH 2/3] Update xml/System/NullReferenceException.xml (pull req suggestion) Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- xml/System/NullReferenceException.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xml/System/NullReferenceException.xml b/xml/System/NullReferenceException.xml index 1334a2751bb..8ac3cce2db2 100644 --- a/xml/System/NullReferenceException.xml +++ b/xml/System/NullReferenceException.xml @@ -154,7 +154,7 @@ :::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/example3.cs" id="Snippet12"::: - To address this issue, make sure that the list is initialized (one way to do this is to call `Activator.CreateInstance` method instead of `FormatterServices.GetUninitializedObject`), or handle the thrown exception in a `try…catch…finally` block. For more information, see [Exceptions](/dotnet/standard/exceptions/). + To address this issue, make sure that the list is initialized (one way to do this is to call `Activator.CreateInstance` instead of `FormatterServices.GetUninitializedObject`), or handle the thrown exception in a `try…catch…finally` block. For more information, see [Exceptions](/dotnet/standard/exceptions/). The following Microsoft intermediate language (MSIL) instructions throw : `callvirt`, `cpblk`, `cpobj`, `initblk`, `ldelem.`, `ldelema`, `ldfld`, `ldflda`, `ldind.`, `ldlen`, `stelem.`, `stfld`, `stind.`, `throw`, and `unbox`. From 8ddebe89bdc090c522175f72bb6dc116ee843389 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Thu, 20 Feb 2025 16:45:47 -0800 Subject: [PATCH 3/3] Apply suggestions from code review --- .../csharp/System/NullReferenceException/Overview/example3.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/csharp/System/NullReferenceException/Overview/example3.cs b/snippets/csharp/System/NullReferenceException/Overview/example3.cs index a070ce1a433..2ce0700f112 100644 --- a/snippets/csharp/System/NullReferenceException/Overview/example3.cs +++ b/snippets/csharp/System/NullReferenceException/Overview/example3.cs @@ -9,7 +9,7 @@ public class NullReferenceExample public static void Main() { var listType = GetListType(); - var listObj = GetList(listType); + _ = GetList(listType); } private static Type GetListType() @@ -29,5 +29,5 @@ private static IList GetList(Type type) // Unhandled Exception: System.NullReferenceException: 'Object reference // not set to an instance of an object.' // at System.Collections.Generic.List`1.System.Collections.IList.Add(Object item) -// at Example.GetList(Type type): line 24 +// at NullReferenceExample.GetList(Type type): line 24 //