I'm trying to get values back from this piece of code. Everything looks okay to me and the deserialization doesn't give any errors so I'm assuming it all went okay there.
If i do a count on the var I get a value back.
Here is my JSON:
{
"Activity": [
{
"ActivityLocation": {
"Address": {
"City": "LA",
"StateProvinceCode": "CA",
"CountryCode": "US"
}
},
"Status": {
"Type": "I",
"Description": "Departure Scan",
"Code": "DP"
},
"Date": "20171201",
"Time": "033400"
},
{
"ActivityLocation": {
"Address": {
"City": "LA",
"StateProvinceCode": "CA",
"CountryCode": "US"
}
},
"Status": {
"Type": "I",
"Description": "Origin Scan",
"Code": "OR"
},
"Date": "20171130",
"Time": "185600"
},
{
"ActivityLocation": {
"Address": {
"CountryCode": "US"
}
},
"Status": {
"Type": "M",
"Description": "Order Processed: Ready for UPS",
"Code": "MP"
},
"Date": "20171129",
"Time": "193858"
}
],}
Here are my classes:
public class Address
{
public string City { get; set; }
public string StateProvinceCode { get; set; }
public string CountryCode { get; set; }
}
public class ActivityLocation
{
public Address Address { get; set; }
}
public class Status
{
public string Type { get; set; }
public string Description { get; set; }
public string Code { get; set; }
}
public class Activity
{
public ActivityLocation ActivityLocation { get; set; }
public Status Status { get; set; }
public string Date { get; set; }
public string Time { get; set; }
}
public class RootObject
{
public List<Activity> Activity { get; set; }
}
var huh = JsonConvert.DeserializeObject<List<RootObject>>(jsonString);
MessageBox.Show(huh[0].Activity[0].ActivityLocation.Address.City);
I expect to get back a string value for city
I get the null reference error.
The exact error is:
System.NullReferenceException: Object reference not set to an instance of an object. at UPSTracking.UPSTrack.<>c_DisplayClass1.b_()(IRestResponse response)