Home > Enterprise >  C# LinkedList: Possible null reference error?
C# LinkedList: Possible null reference error?

Time:04-26

I'm trying to learn some basics of C# (not new to programming in general) and I'm having trouble making some code for a LinkedList work properly. Specifically, I'm getting a "possible null reference" error when assigned a value from the LinkedList to LinkedListNode variable - is it not possible to do this? I got the code off a website (https://dev.to/adavidoaiei/fundamental-data-structures-and-algorithms-in-c-4ocf) so it seems odd to me that the code would be completely wrong.

This is a section of the code causing issues:

    // Create the linked list
    string[] words = 
        { "the", "fox", "jumps", "over", "the", "dog" };
    LinkedList<string> sentence = new LinkedList<string>(words);
    Display(sentence, "The linked list values:");
    Console.WriteLine("sentence.Contains(\"jumps\") = {0}",
        sentence.Contains("jumps"));

    // Add the word 'today' to the beginning of the linked list
    sentence.AddFirst("today");
    Display(sentence, "Test 1: Add 'today' to beginning of the list:");

    // Move the first node to be the last node
    LinkedListNode<string> mark1 = sentence.First;
    sentence.RemoveFirst();
    sentence.AddLast(mark1);
    Display(sentence, "Test 2: Move first node to be last node:");

When initialising the variable 'mark1', I'm getting a "possible null reference error":

LinkedListNode<string> mark1 = sentence.First;

Is what I'm trying to do possible at all or is this the completely wrong way to work with a LinkedList?

CodePudding user response:

Take a look at the signature for the ListedList<T>.First property:

public System.Collections.Generic.LinkedListNode<T>? First { get; }

See that question mark? That means that the value can be null.

The warning you're getting (it's a warning, not an error) is telling you that when you go to read the First property, it could have a null value, and you could be assigning a null value to something that shouldn't have a null value assigned to it.

Now, unfortunately the current c# compiler isn't smart enough to recognize that something like this can't have null with an example like this:

LinkedList<string> sentence = new LinkedList<string>();
sentence.AddFirst("today");
string a = sentence.First.Value;
// last line will have a warning for "sentence.First" possibly being null.

Three options to fix this.

  1. "I know better". Use the "null forgiveness" operator (!) after the thing that the compiler thinks could be null to force it to treat it as always not-null.

    string a = sentence.First!.Value;
    
  2. "Check it". Do a simple null check beforehand.

    if (sentence.First is not null)
        string a = sentence.First.Value;
    
  3. Disable nullable reference types feature. I really don't recommend this.


Also, general warning: all of this is compile time checks. It has no power for null checking/enforcement when the program is running. A variable marked as "non-nullable" can be set to null when the program runs.

  • Related