Click here to Skip to main content
15,039,359 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to fetch the data from rest api but getting null from the result.data

        request4.AddHeader("Content-Type", "application/json");
        request4.AddHeader("Authorization", "Bearer " + mytoken);
        request4.AddParameter("application/json", "{ \"active\": true }", ParameterType.RequestBody);


        var result = client4.Execute<List<RootObject>>(request4);//getting nullexcception here

        //Console.WriteLine(result);
        //Console.ReadKey();

        foreach (var i in result.Data)

        {
            foreach (var j in i.sales_offices)
            {
                Console.WriteLine(j.state);
            }
}

        }


    public class SalesOffice
    {
        public bool active { get; set; }
        public string _id { get; set; }
        public string name { get; set; }
        public string state { get; set; }
    }


public class RootObject
      {
          public bool success { get; set; }
          public string message { get; set; }
          public List<SalesOffice> sales_offices { get; set; }

      }
and from json I'm getting this result

{ "success": true, "message": "Gtm Cities loaded successfully!", "sales_offices": [ { "active": true, "_id": "XXXXX5cb57542ed50d82730d7661d", "name": "UAXXS", "state": "AXXX.P" }

Please help me as it is showing no issue in the code


What I have tried:

<pre>I'm trying to fetch the data from rest api but getting null from the result.data

        request4.AddHeader("Content-Type", "application/json");
        request4.AddHeader("Authorization", "Bearer " + mytoken);
        request4.AddParameter("application/json", "{ \"active\": true }", ParameterType.RequestBody);


        var result = client4.Execute<List<RootObject>>(request4);//getting nullexcception here

        //Console.WriteLine(result);
        //Console.ReadKey();

        foreach (var i in result.Data)

        {
            foreach (var j in i.sales_offices)
            {
                Console.WriteLine(j.state);
            }
}

        }


    public class SalesOffice
    {
        public bool active { get; set; }
        public string _id { get; set; }
        public string name { get; set; }
        public string state { get; set; }
    }


public class RootObject
      {
          public bool success { get; set; }
          public string message { get; set; }
          public List<SalesOffice> sales_offices { get; set; }

      }
and from json I'm getting this result

{ "success": true, "message": "Gtm Cities loaded successfully!", "sales_offices": [ { "active": true, "_id": "XXXXX5cb57542ed50d82730d7661d", "name": "UAXXS", "state": "AXXX.P" }

Please help me as it is showing no issue in the code
Posted
Updated 8-Apr-20 15:43pm
Comments
Richard MacCutchan 8-Apr-20 12:17pm
   
Use your debugger to see which reference is throwing the exception.

1 solution

This

var result = client4.Execute<List<RootObject>>(request4);


looks incorrect - you are not returning a list of RootObjects, you're (hopefully) returning a RootObject hierarchy that has within it, a list of SalesOffice(s)

Your data shows that at least you're getting a SalesOffice, I'm not sure/it's not clear if that's just a snippet of data you've shown or the entire return - If result is being populated correctly, then, I would expect to access the first sales_office by something like:

SalesOffice soFirst = result.sales_offices.first();


btw 'RootObject' as you have defined does not have a 'Data' field/element, so this wont work - I suspect what you meant was more along the lines of

foreach (var j in result.sales_offices)
{
    Console.WriteLine(j.state);
}



I must admit, I hate the use of 'var j' there, since you know it's type ie 'SalesOffice' .. overusing var makes code hard to read
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900