C
C#2y ago
EddieV

❔ Non-nullable property must contain a non-null value when exiting constructor

I know what it says and what it wants my to do. However, my situation is: I have a bunch of objects (as properties) and that not all of them needs instantiating in a main class constructor. Given a condition, some will be and some won't be. In case of one that won't be, I still have no idea how to make the warning go away. Any suggestion? I'm sure how to Google this since using the warning would send me to a non-desirable direction.
7 Replies
Thinker
Thinker2y ago
If some properties might not be set, then make them nullable
EddieV
EddieVOP2y ago
@🌈 Thinker 🌈 I did, however the Console still warns me about null
Thinker
Thinker2y ago
Can you show your code?
Tinefol
Tinefol2y ago
Mind providing code snippet?
EddieV
EddieVOP2y ago
public class World
{
public int[,] WorldState { get; set; }
public Fountain Fountain { get; set; }
public Pit[] Pits { get; set; }
public Maelstrom?[] Maelstroms { get; set; }
public Entrance Entrance { get; set; } = new Entrance();

public World(string? size)
{

switch (size)
{
case "small":
WorldState = new int[4, 4];
Fountain = new Fountain(new Point(3, 1));
Pits = new Pit[1] { new Pit(new Point(1, 1)) };
break;

case "medium":
WorldState = new int[6, 6];
Fountain = new Fountain(new Point(4, 3));
Pits = new Pit[2]
{
new Pit(new Point(3, 1)),
new Pit(new Point(2, 3))
};
Maelstroms = new Maelstrom[1] { new Maelstrom(new Point(4, 2))};
break;

case "large":
WorldState = new int[8, 8];
Fountain = new Fountain(new Point(4, 3));
Pits = new Pit[2]
{
new Pit(new Point(3, 1)),
new Pit(new Point(2, 3))
};
Maelstroms = new Maelstrom[1] { new Maelstrom(new Point(4, 2))};
break;

default:
throw new NotSupportedException("Invalid world size.");

}

}
public class World
{
public int[,] WorldState { get; set; }
public Fountain Fountain { get; set; }
public Pit[] Pits { get; set; }
public Maelstrom?[] Maelstroms { get; set; }
public Entrance Entrance { get; set; } = new Entrance();

public World(string? size)
{

switch (size)
{
case "small":
WorldState = new int[4, 4];
Fountain = new Fountain(new Point(3, 1));
Pits = new Pit[1] { new Pit(new Point(1, 1)) };
break;

case "medium":
WorldState = new int[6, 6];
Fountain = new Fountain(new Point(4, 3));
Pits = new Pit[2]
{
new Pit(new Point(3, 1)),
new Pit(new Point(2, 3))
};
Maelstroms = new Maelstrom[1] { new Maelstrom(new Point(4, 2))};
break;

case "large":
WorldState = new int[8, 8];
Fountain = new Fountain(new Point(4, 3));
Pits = new Pit[2]
{
new Pit(new Point(3, 1)),
new Pit(new Point(2, 3))
};
Maelstroms = new Maelstrom[1] { new Maelstrom(new Point(4, 2))};
break;

default:
throw new NotSupportedException("Invalid world size.");

}

}
Here's the code. I'm doing an exercise, if player chooses "small", there won't be maelstroms in the game So the console tells me, Maelstrom must not be null when existing in the constructor.
Tinefol
Tinefol2y ago
You need ? after the [], not before the way its written by you is an non-nullable array of nullables you need nullable array though
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server