Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.NullReferenceException F# snippets #7802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Array1

// <Snippet8>
open System

let values = [| "one"; null; "two" |]
for i = 0 to values.GetUpperBound 0 do
printfn $"""{values[i].Trim()}{if i = values.GetUpperBound 0 then "" else ", "}"""
printfn ""
// The example displays the following output:
// Unhandled Exception:
// System.NullReferenceException: Object reference not set to an instance of an object.
// at <StartupCode$fs>.main()
// </Snippet8>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Array2

// <Snippet9>
open System

let values = [| "one"; null; "two" |]
for i = 0 to values.GetUpperBound 0 do
printf $"""{if values[i] <> null then values[i].Trim() else ""}{if i = values.GetUpperBound 0 then "" else ", "}"""
Console.WriteLine()
// The example displays the following output:
// one, , two
// </Snippet9>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Array3

// <Snippet10>
let values: int[] = null
for i = 0 to 9 do
values[i] <- i * 2

for value in values do
printfn $"{value}"
// The example displays the following output:
// Unhandled Exception:
// System.NullReferenceException: Object reference not set to an instance of an object.
// at <StartupCode$fs>.main()
// </Snippet10>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Array4

// <Snippet11>
let values = Array.zeroCreate<int> 10
for i = 0 to 9 do
values[i] <- i * 2

for value in values do
printfn $"{value}"
// The example displays the following output:
// 0
// 2
// 4
// 6
// 8
// 10
// 12
// 14
// 16
// 18
// </Snippet11>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Chain1

// <Snippet6>
open System

type Page() =
[<DefaultValue>]
val mutable public URL: Uri
[<DefaultValue>]
val mutable public Title: string

type Pages() =
let pages = Array.zeroCreate<Page> 10
let mutable i = 0

member _.CurrentPage
with get () = pages[i]
and set (value) =
// Move all the page objects down to accommodate the new one.
if i > pages.GetUpperBound 0 then
for ndx = 1 to pages.GetUpperBound 0 do
pages[ndx - 1] <- pages[ndx]

pages[i] <- value
if i < pages.GetUpperBound 0 then
i <- i + 1

member _.PreviousPage =
if i = 0 then
if box pages[0] = null then
Unchecked.defaultof<Page>
else
pages[0]
else
i <- i - 1
pages[i + 1]

let pages = Pages()
if String.IsNullOrEmpty pages.CurrentPage.Title |> not then
let title = pages.CurrentPage.Title
printfn $"Current title: '{title}'"


// The example displays the following output:
// Unhandled Exception:
// System.NullReferenceException: Object reference not set to an instance of an object.
// at <StartupCode$fs>.main()
// </Snippet6>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Chain2

type Page() =
[<DefaultValue>]
val mutable public URL: System.Uri
[<DefaultValue>]
val mutable public Title: string

type Pages() =
let pages = Array.zeroCreate<Page> 10
let mutable i = 0

member _.CurrentPage
with get () = pages[i]
and set (value) =
// Move all the page objects down to accommodate the new one.
if i > pages.GetUpperBound 0 then
for ndx = 1 to pages.GetUpperBound 0 do
pages[ndx - 1] <- pages[ndx]

pages[i] <- value
if i < pages.GetUpperBound 0 then
i <- i + 1

member _.PreviousPage =
if i = 0 then
if box pages[0] = null then
Unchecked.defaultof<Page>
else
pages[0]
else
i <- i - 1
pages[i + 1]

// <Snippet7>
let pages = Pages()
let current = pages.CurrentPage
if box current <> null then
let title = current.Title
printfn $"Current title: '{title}'"
else
printfn "There is no page information in the cache."
// The example displays the following output:
// There is no page information in the cache.
// </Snippet7>

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Example

// <Snippet1>
open System

[<EntryPoint>]
let main args =
let value = Int32.Parse args[0]
// Set names to null, don't initialize it.
let mutable names = Unchecked.defaultof<ResizeArray<string>>
if value > 0 then
names <- ResizeArray()
names.Add "Major Major Major"
0
// Compilation does not display a warning as this is an extremely rare occurance in F#.
// Creating a value without initalizing either requires using 'null' (not possible
// on types defined in F# without [<AllowNullLiteral>]) or Unchecked.defaultof.
//
// The example displays output like the following output:
// Unhandled Exception: System.NullReferenceException: Object reference
// not set to an instance of an object.
// at Example.main()
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module example1a

// <Snippet2>
let names = ResizeArray()
names.Add "Major Major Major"
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module example2

// <Snippet3>
let populateNames (names: ResizeArray<string>) =
let arrNames =
[ "Dakota"; "Samuel"; "Nikita"
"Koani"; "Saya"; "Yiska"; "Yumaevsky" ]
for arrName in arrNames do
names.Add arrName

let getData () : ResizeArray<string> =
null

let names = getData ()
populateNames names

// The example displays output like the following:
// Unhandled Exception: System.NullReferenceException: Object reference
// not set to an instance of an object.
// at Example.PopulateNames(List`1 names)
// at <StartupCode$fs>.main()
// </Snippet3>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="example1a.fs" />
<Compile Include="Array3.fs" />
<Compile Include="Array4.fs" />
<Compile Include="nullreturn2.fs" />
<Compile Include="Chain1.fs" />
<Compile Include="Chain2.fs" />
<Compile Include="Array1.fs" />
<Compile Include="Array2.fs" />
<Compile Include="example2.fs" />
<Compile Include="nullreturn2a.fs" />
<Compile Include="example1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module nullreturn2

// <Snippet4>
open System

type Person(firstName) =
member _.FirstName = firstName

static member AddRange(firstNames) =
Array.map Person firstNames

let persons =
[| "Abigail"; "Abra"; "Abraham"; "Adrian"
"Ariella"; "Arnold"; "Aston"; "Astor" |]
|> Person.AddRange

let nameToFind = "Robert"
let found = Array.Find(persons, fun p -> p.FirstName = nameToFind)

printfn $"{found.FirstName}"

// The example displays the following output:
// Unhandled Exception: System.NullReferenceException:
// Object reference not set to an instance of an object.
// at <StartupCode$fs>.main()
// </Snippet4>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module nullreturn2a

// <Snippet5>
open System

[<AllowNullLiteral>]
type Person(firstName) =
member _.FirstName = firstName

static member AddRange(firstNames) =
Array.map Person firstNames

let persons =
[| "Abigail"; "Abra"; "Abraham"; "Adrian"
"Ariella"; "Arnold"; "Aston"; "Astor" |]
|> Person.AddRange

let nameToFind = "Robert"
let found = Array.Find(persons, fun p -> p.FirstName = nameToFind)

if found <> null then
printfn $"{found.FirstName}"
else
printfn $"{nameToFind} not found."

// Using F#'s Array.tryFind function
// This does not require a null check or [<AllowNullLiteral>]
let found2 =
persons |> Array.tryFind (fun p -> p.FirstName = nameToFind)

match found2 with
| Some firstName ->
printfn $"{firstName}"
| None ->
printfn $"{nameToFind} not found."

// The example displays the following output:
// Robert not found.
// Robert not found.
// </Snippet5>
11 changes: 11 additions & 0 deletions xml/System/NullReferenceException.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,25 @@
- You've forgotten to instantiate a reference type. In the following example, `names` is declared but never instantiated:

:::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/example1.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/NullReferenceException/Overview/example1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.nullreferenceexception.class/vb/example1.vb" id="Snippet1":::

Some compilers issue a warning when they compile this code. Others issue an error, and the compilation fails. To address this problem, instantiate the object so that its value is no longer `null`. The following example does this by calling a type's class constructor.

:::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/example1a.cs" id="Snippet2":::
:::code language="fsharp" source="~/snippets/fsharp/System/NullReferenceException/Overview/example1a.fs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.nullreferenceexception.class/vb/example1a.vb" id="Snippet2":::

- You've forgotten to dimension an array before initializing it. In the following example, `values` is declared to be an integer array, but the number of elements that it contains is never specified. The attempt to initialize its values therefore thrown a <xref:System.NullReferenceException> exception.

:::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/Array3.cs" id="Snippet10":::
:::code language="fsharp" source="~/snippets/fsharp/System/NullReferenceException/Overview/Array3.fs" id="Snippet10":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.nullreferenceexception.class/vb/Array3.vb" id="Snippet10":::

You can eliminate the exception by declaring the number of elements in the array before initializing it, as the following example does.

:::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/Array4.cs" id="Snippet11":::
:::code language="fsharp" source="~/snippets/fsharp/System/NullReferenceException/Overview/Array4.fs" id="Snippet11":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.nullreferenceexception.class/vb/Array4.vb" id="Snippet11":::

For more information on declaring and initializing arrays, see [Arrays](/dotnet/csharp/programming-guide/arrays/) and [Arrays](/dotnet/visual-basic/programming-guide/language-features/arrays/).
Expand All @@ -90,40 +94,47 @@
The code in the following example assumes that the <xref:System.Array.Find%2A?displayProperty=nameWithType> method always returns `Person` object whose `FirstName` field matches a search string. Because there is no match, the runtime throws a <xref:System.NullReferenceException> exception.

:::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/nullreturn2.cs" id="Snippet4":::
:::code language="fsharp" source="~/snippets/fsharp/System/NullReferenceException/Overview/nullreturn2.fs" id="Snippet4":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.nullreferenceexception.class/vb/nullreturn2.vb" id="Snippet4":::

To address this problem, test the method's return value to ensure that it is not `null` before calling any of its members, as the following example does.

:::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/nullreturn2a.cs" id="Snippet5":::
:::code language="fsharp" source="~/snippets/fsharp/System/NullReferenceException/Overview/nullreturn2a.fs" id="Snippet5":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.nullreferenceexception.class/vb/nullreturn2a.vb" id="Snippet5":::

- You're using an expression (for example, you're chaining a list of methods or properties together) to retrieve a value and, although you're checking whether the value is `null`, the runtime still throws a <xref:System.NullReferenceException> exception. This occurs because one of the intermediate values in the expression returns `null`. As a result, your test for `null` is never evaluated.

The following example defines a `Pages` object that caches information about web pages, which are presented by `Page` objects. The `Example.Main` method checks whether the current web page has a non-null title and, if it does, displays the title. Despite this check, however, the method throws a <xref:System.NullReferenceException> exception.

:::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/Chain1.cs" id="Snippet6":::
:::code language="fsharp" source="~/snippets/fsharp/System/NullReferenceException/Overview/Chain1.fs" id="Snippet6":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.nullreferenceexception.class/vb/Chain1.vb" id="Snippet6":::

The exception is thrown because `pages.CurrentPage` returns `null` if no page information is stored in the cache. This exception can be corrected by testing the value of the `CurrentPage` property before retrieving the current `Page` object's `Title` property, as the following example does:

:::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/Chain2.cs" id="Snippet7":::
:::code language="fsharp" source="~/snippets/fsharp/System/NullReferenceException/Overview/Chain2.fs" id="Snippet7":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.nullreferenceexception.class/vb/Chain2.vb" id="Snippet7":::

- You're enumerating the elements of an array that contains reference types, and your attempt to process one of the elements throws a <xref:System.NullReferenceException> exception.

The following example defines a string array. A `for` statement enumerates the elements in the array and calls each string's <xref:System.String.Trim%2A> method before displaying the string.

:::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/Array1.cs" id="Snippet8":::
:::code language="fsharp" source="~/snippets/fsharp/System/NullReferenceException/Overview/Array1.fs" id="Snippet8":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.nullreferenceexception.class/vb/Array1.vb" id="Snippet8":::

This exception occurs if you assume that each element of the array must contain a non-null value, and the value of the array element is in fact `null`. The exception can be eliminated by testing whether the element is `null` before performing any operation on that element, as the following example shows.

:::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/Array2.cs" id="Snippet9":::
:::code language="fsharp" source="~/snippets/fsharp/System/NullReferenceException/Overview/Array2.fs" id="Snippet9":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.nullreferenceexception.class/vb/Array2.vb" id="Snippet9":::

- A <xref:System.NullReferenceException> exception is thrown by a method that is passed `null`. Some methods validate the arguments that are passed to them. If they do and one of the arguments is `null`, the method throws an <xref:System.ArgumentNullException?displayProperty=nameWithType> exception. Otherwise, it throws a <xref:System.NullReferenceException> exception. The following example illustrates this scenario.

:::code language="csharp" source="~/snippets/csharp/System/NullReferenceException/Overview/example2.cs" id="Snippet3":::
:::code language="fsharp" source="~/snippets/fsharp/System/NullReferenceException/Overview/example2.fs" id="Snippet3":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.nullreferenceexception.class/vb/example2.vb" id="Snippet3":::

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/).
Expand Down