© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
12 replies
.logik.

✅ Help with non-nullable field must contain a non-null value when exiting constructor.

Following an online tutorial and trying to resolve nullable reference warnings (the tutorial was before nullable reference types were introduced).

I have a
GameSession
GameSession
class with the constructor defined as
public GameSession()
        {
            CurrentPlayer = new Player
            {
                Name = "MyName",
                Gold = 1000000,
                CharacterClass = "Ranger",
                HitPoints = 10,
                ExperiencePoints = 0,
                Level = 1,
            };

            CurrentWorld = WorldFactory.CreateWorld();

            CurrentLocation = CurrentWorld.LocationAt(0, -1);
        }
public GameSession()
        {
            CurrentPlayer = new Player
            {
                Name = "MyName",
                Gold = 1000000,
                CharacterClass = "Ranger",
                HitPoints = 10,
                ExperiencePoints = 0,
                Level = 1,
            };

            CurrentWorld = WorldFactory.CreateWorld();

            CurrentLocation = CurrentWorld.LocationAt(0, -1);
        }


The error is regarding the backing field
private Location _currentLocation
private Location _currentLocation
which is set using the property
public Location CurrentLocation
        {
            get => _currentLocation;
            set
            {
                _currentLocation = value;
                OnPropertyChanged();
            }
        }
public Location CurrentLocation
        {
            get => _currentLocation;
            set
            {
                _currentLocation = value;
                OnPropertyChanged();
            }
        }

Furthermore the
LocationAt
LocationAt
method is non-nullable defined as
public Location LocationAt(int xCoordinate, int yCoordinate)
        {
            foreach (Location loc in _locations)
            {
                if (loc.XCoordinate == xCoordinate && loc.YCoordinate == yCoordinate)
                {
                    return loc;
                }
            }

            throw new LocationNotFoundException($"Location at x:{xCoordinate} y:{yCoordinate} does not exist");
        }
public Location LocationAt(int xCoordinate, int yCoordinate)
        {
            foreach (Location loc in _locations)
            {
                if (loc.XCoordinate == xCoordinate && loc.YCoordinate == yCoordinate)
                {
                    return loc;
                }
            }

            throw new LocationNotFoundException($"Location at x:{xCoordinate} y:{yCoordinate} does not exist");
        }


So my question is, why am I still getting the warning and what is the best approach to fixing it?

As I understand it, the compiler already knows that
LocationAt
LocationAt
will return a
Location
Location
and the backing field
_currentLocation
_currentLocation
should be set by the
CurrentLocation
CurrentLocation
property in the constructor. Why does it still think
currentLocation
currentLocation
is null when exiting constructor?

TIA
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ Non-nullable field 'rating' must contain a non-null value when exiting constructor
C#CC# / help
4y ago
❔ Non-nullable property must contain a non-null value when exiting constructor
C#CC# / help
3y ago
❔ Non-nullable property 'Name' must contain a non-null value when exiting constructor
C#CC# / help
3y ago
getting warning: CS8618 - Non-nullable variable must contain a non-null value when exiting construct
C#CC# / help
3y ago