Last post Jun 29, 2020 10:41 PM by PaulTheSmith
Member
114 Points
229 Posts
Jun 29, 2020 12:46 PM|itismesa|LINK
Hi
How can I check for NullReferenceException that happened sometimes when I run the following code.
sourcejson= JsonSerializer.Deserialize<SourceJson>(Contnt); string s=sourcejson.Address.City;
When I Deserialize JSON string that received from external source, it works fine most of the times.
However, when there is no Address information for some records, it generate NullReferenceException.
I tried to write it this way but received the same error
string s=sourcejson.Address.City + "" ;
What is the best way to handle this exception
regards
Participant
1061 Points
666 Posts
Jun 29, 2020 01:40 PM|jzero|LINK
Place Deserialize inside a Use Try/Catch blok https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch
Jun 29, 2020 05:00 PM|itismesa|LINK
The issue does not come from the Deserialize statement.
It comes when i tried to assign the address object. Thank u anyway
All-Star
53051 Points
23644 Posts
Jun 29, 2020 05:13 PM|mgebhard|LINK
itismesa However, when there is no Address information for some records, it generate NullReferenceException.
Write a basic "if" condition to check for null.
if (sourcejson.Address == null) { //do something }
Or use the null-conditional operator in C# version 6 and above.
sourcejson?.Address?.City
1620 Points
929 Posts
Jun 29, 2020 10:41 PM|PaulTheSmith|LINK
Since c# V6 you can write
string s = sourcejson.Address?.City;
?. is the null conditional operator (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-). If .Address is null then the .City part will not be evaluated and the whole expression returns null.
Bottom line
If source.Address is not null then s is set to the value of City
if source.Address is null then s is set to null.
** Just noticed someone already gave this answer. Shouldn't stop for coffee in the middle of posting **
Member
114 Points
229 Posts
How to Handle NullReferenceException
Jun 29, 2020 12:46 PM|itismesa|LINK
Hi
How can I check for NullReferenceException that happened sometimes when I run the following code.
sourcejson= JsonSerializer.Deserialize<SourceJson>(Contnt);
string s=sourcejson.Address.City;
When I Deserialize JSON string that received from external source, it works fine most of the times.
However, when there is no Address information for some records, it generate NullReferenceException.
I tried to write it this way but received the same error
string s=sourcejson.Address.City + "" ;
What is the best way to handle this exception
regards
Participant
1061 Points
666 Posts
Re: How to Handle NullReferenceException
Jun 29, 2020 01:40 PM|jzero|LINK
Place Deserialize inside a Use Try/Catch blok
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch
Member
114 Points
229 Posts
Re: How to Handle NullReferenceException
Jun 29, 2020 05:00 PM|itismesa|LINK
The issue does not come from the Deserialize statement.
It comes when i tried to assign the address object.
Thank u anyway
All-Star
53051 Points
23644 Posts
Re: How to Handle NullReferenceException
Jun 29, 2020 05:13 PM|mgebhard|LINK
Write a basic "if" condition to check for null.
Or use the null-conditional operator in C# version 6 and above.
Participant
1620 Points
929 Posts
Re: How to Handle NullReferenceException
Jun 29, 2020 10:41 PM|PaulTheSmith|LINK
Since c# V6 you can write
string s = sourcejson.Address?.City;
?. is the null conditional operator (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-). If .Address is null then the .City part will not be evaluated and the whole expression returns null.
Bottom line
If source.Address is not null then s is set to the value of City
if source.Address is null then s is set to null.
** Just noticed someone already gave this answer. Shouldn't stop for coffee in the middle of posting **