Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

Null check operator used on a null value

For example:

String? foo; // Nullable String

void main() {
  var len = foo!.length; // Runtime error: Null check operator used on a null value
}
Solutions:
You need to find out where you're using the bang operator in your code. Once you are there, you can use any of the following solutions:

Use a local variable

var f = foo;
if (f != null) {
  var len = f.length; // Safe 
}
Use ?. and ??

var len = foo?.length ?? 0; // Provide a default value if foo was null.
 
PREVIOUS NEXT
Tagged: #Null #check #operator #null
ADD COMMENT
Topic
Name
7+1 =