Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetCore 6 - Converting null literal or possible null value to non-nullable type

I updated a project to NetCore 6 and I am getting the warning:

Converting null literal or possible null value to non-nullable type.

For example on a unit test:

String source = null;
String expect = null;

String actual = source.ToSafeBase64Url(); 

I am getting this warning in multiple places of my code.

How should I solve this?

like image 212
Miguel Moura Avatar asked Oct 13 '25 10:10

Miguel Moura


1 Answers

In the new .NET6 templates, nullable reference types are enabled by default. You can see this if you open the csproj file, the line:

<Nullable>enable</Nullable>

That warning is telling you that you are assigning a null to a non-nullable type. To fix it, make the strings nullable:

string? source = null;
string? expect = null;
like image 158
DavidG Avatar answered Oct 15 '25 01:10

DavidG