I'm trying to map API JSON data into some properties of a class, and I'm getting the error below. I was able to narrow down the data entry causing this issue. [The value for PositionDateStart]
The script successfully creates an object off the properties, but once I call the object to test it using a foreach
loop, it crashes and gives me this error.
My question is what could I write to tell the script to replace null with today's date if this comes up.
Thanks in advance!
Error Message:
NullReferenceException: Object reference not set to an instance of an object.
JSON Results @Console.WriteLine(json)
[
{
"EntryID": 41992,
"Position": "Associate",
"PositionDateEnd": "2020-05-15T00:00:00",
"PositionDateStart": null
}
]
Script
var json = await response.Content.ReadAsStringAsync();
Console.WriteLine(json); //JSON results above
var result = JsonConvert.DeserializeObject<List<Properties>>(json);
//Point at which code errors outs
foreach (var item in result)
{
Console.WriteLine(item.PositionDateStart);
Console.WriteLine(item.PositionDateStart.GetType());
}
Class Properties
public class Properties
{
public int EntryID { get; set; }
public string Position { get; set; }
public DateTime? PositionDateEnd { get; set; }
public DateTime? PositionDateStart { get; set; }
}