none
C# class for JSON is resulting a Null Reference Exception RRS feed

  • Question

  •  

    I am deserializing an object which is giving an exception as below:

    ------------C# Class for JSON---------

    public class TimeseriesAPI
    {

        public partial class DataPoints
        {
            [JsonProperty("SERVICE-5F4932A98C0A1FD4")]
            public double?[][] Service5F4932A98C0A1Fd4 { get; set; }
        }

        public partial class Entities
        {
            [JsonProperty("SERVICE-5F4932A98C0A1FD4")]
            public string Service5F4932A98C0A1Fd4 { get; set; }
        }

        public class Result
    {

        [JsonProperty("dataPoints")]
        public DataPoints dataPoints { get; set; }

        [JsonProperty("timeseriesId")]
        public string timeseriesId { get; set; }

        [JsonProperty("unit")]
        public string unit { get; set; }

        [JsonProperty("entities")]
        public Entities entities { get; set; }

        [JsonProperty("resolutionInMillisUTC")]
        public int resolutionInMillisUTC { get; set; }

        [JsonProperty("aggregationType")]
        public string aggregationType { get; set; }
    }

    public class Timeseries
    {

        [JsonProperty("result")]
        public Result result { get; set; }
    }

    -----------JSON------------

    {
        "result": {
            "dataPoints": {
                "SERVICE-5F4932A98C0A1FD4": [
                    [
                        1522013280000,
                        7782.208333333333
                    ],
                    [
                        1522016760000,
                        8163.104166666667
                    ],
                    [
                        1522016820000,
                        null
                    ],
                    [
                        1522016880000,
                        null
                    ]
                ]
            },
            "timeseriesId": "com.dynatrace.builtin:service.responsetime",
            "unit": "MicroSecond (µs)",
            "entities": {
                "SERVICE-5F42A98C0A1FD4": "Ecent"
            },
            "resolutionInMillisUTC": 60000,
            "aggregationType": "AVG"
        }
    }

    --------------Operation resulting in exception (c#)----------

     TimeseriesAPI.Result stuff = JsonConvert.DeserializeObject<TimeseriesAPI.Result>(respStr);
            String Agg = stuff.aggregationType;
            String Unit = stuff.unit;
            String EntityName = stuff.entities.Service5F4932A98C0A1Fd4;
            bool  hasValue =  stuff.dataPoints.Service5F4932A98C0A1Fd4[0][0].HasValue;
            LblInfoText.Text = Agg + Unit  + EntityName  ;

    --------Exception---------------

    System.NullReferenceException: Object reference not set to an instance of an object. on the above lines marked in bold.

    Sunday, March 25, 2018 11:19 PM

All replies

  • Hello abhi1507,

    >>System.NullReferenceException: Object reference not set to an instance of an object. on the above lines marked in bold.

    The exception occurs when you are trying to access null object and the null object is a result of failed deserialize. So the root of cause is the json string doesn't match class structure. Try the below code.

     var stuff = JsonConvert.DeserializeObject<TimeseriesAPI.Timeseries>(respStr);
     String Agg = stuff.result.aggregationType;
     String Unit = stuff.result.unit;
     String EntityName = stuff.result.entities.Service5F4932A98C0A1Fd4;
     bool hasValue = stuff.result.dataPoints.Service5F4932A98C0A1Fd4[0][0].HasValue;
     var result = Agg + Unit + EntityName;

    Replace Entities class with below(change match value of JsonProperty attribute)

     public partial class Entities
            {
                [JsonProperty("SERVICE-5F42A98C0A1FD4")]
                public string Service5F4932A98C0A1Fd4 { get; set; }
            }

    Note. If you want to convert Json string to class, you could try to use VS feature as below.

    JSON to C# class conversion using Paste Special in Visual Studio

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 26, 2018 2:23 AM
    Moderator