-1

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; }
}
natn2323
  • 1,826
  • 1
  • 9
  • 24

3 Answers3

1

You can simply assign current date if PositionDateStart is null then output to console anyway without errors since it will never be empty:

//Point at which code errors outs 
foreach (var item in result)
{
   if (item.PositionDateStart == null)
       item.PositionDateStart = DateTime.Now;
   Console.WriteLine(item.PositionDateStart);
   Console.WriteLine(item.PositionDateStart.GetType());
}

EDIT

As asked in comment, if you prefere to modify the class itself here is a variant through the get accessor so your foreach remains the same:

public class Properties
{
    public int EntryID { get; set; }

    public string Position { get; set; }

    public DateTime? PositionDateEnd { get; set; }

    private DateTime? _positionDateStart;
    public DateTime? PositionDateStart
    {
       get { return _positionDateStart == null ? DateTime.Now : _positionDateStart; }
       set { _positionDateStart = value; }
    }
}
TaiT's
  • 2,532
  • 2
  • 13
  • 25
  • Thank you. This worked. Do you know if it'd be possible to set this condition to the property in the Class Properties? – ChunkyFresh Aug 07 '19 at 17:08
  • 1
    @ChunkyFresh You should be able to do that. See my edits. And also pls approve the answer, that's already two ;). Happy coding! – TaiT's Aug 07 '19 at 17:28
  • You got it. I tried this at the class level,and it worked perfectly. One last thing thoug. I'm fairly new to C# sharp so I was wondering if you could point me to what I could read to understand the syntax you provided? Having a tough time deciphering what's going on inside the get/set part of it. – ChunkyFresh Aug 07 '19 at 18:49
  • The `?:` syntax is called the [conditional operator](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator). You can see it as an if/else in one single line. – TaiT's Aug 07 '19 at 22:05
  • Thank you. Appreciate all the help! – ChunkyFresh Aug 08 '19 at 14:54
0

Try (not tested):

Console.WriteLine(item?.PositionDateStart ?? DateTime.Now);

The null condional and null coalese operators are explained here

Peter Smith
  • 5,321
  • 8
  • 46
  • 72
0

try the following code:

foreach (var item in result)
{
    if(item.PositionStartDate == null)
    {
        item.PositionDateStart = DateTime.Now;
    }
    Console.WriteLine(item.PositionDateStart.GetType());
}
Örvar
  • 1,030
  • 8
  • 20