Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Object reference not set to an instance of an object in JSON C#

$
0
0

Why i keep getting the above error when i retrieve out the flight amount?

below is my code for reference.

    var requestBooking = (HttpWebRequest)WebRequest.Create("http://www.wego.com/api/flights/pull.html?format=json&apiKey=" + apiKey + "&instanceId=08b2fe006fcd9cd9eff376c87dcf4b05e16a0941&rand=1");
        var webResponse = (HttpWebResponse)requestBooking.GetResponse();
        if (webResponse.StatusCode == HttpStatusCode.OK)
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            StreamReader sr = new StreamReader(webResponse.GetResponseStream());
            string resString = sr.ReadToEnd();
            RootObject ro = new RootObject();
            ro = json.Deserialize<RootObject>(resString);
            string flightClass = "";
            string originCountryCode = "";
            string destinationCountryCode = "";
            string price = "";
            Price p = new Price();
            foreach (Itinerary i in ro.response.itineraries)
            {
                originCountryCode = i.originCountryCode;
                destinationCountryCode = i.destinationCountryCode;
                p = i.price;
                price = p.totalAmount;
                //foreach (FlightClass fc in inboundInfo.flightClasses)
                //{
                //}
                Debug.Write("\n " + originCountryCode + "\n " + destinationCountryCode + price);
                Label3.Text += "\n From: " + originCountryCode + "\n To: " + destinationCountryCode + price;
            }

the error point is at price = p.totalAmount;


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>