如何在Restsharp中的Web Api响应中处理NullReferenceException

时间:2019-01-31 02:02:00

标签: restsharp

我收到“ System.NullReferenceException:'对象引用未设置为对象的实例。”响应返回1个或多个空值时出错,否则效果很好。处理该异常的最佳方法是什么,以便程序继续执行?

    var client = new RestClient("https://www.edsm.net/api-v1/system");
    var request = new RestRequest("resource/");
    request.AddParameter("systemName", "1 Ceti");
    request.AddParameter("showInformation", "1");
    var response = client.Post<RootObject>(request);

    var all = response.Data.information.allegiance;
    var fact = response.Data.information.faction
    var gov = response.Data.information.government;
    var pop = response.Data.information.population;

    Console.WriteLine(all);
    Console.WriteLine(fact);
    Console.WriteLine(gov);
    Console.WriteLine(pop);

0 个答案:

没有答案