简体   繁体   English

无法对 null 引用执行运行时绑定 var 类型的异常

[英]Cannot perform runtime binding on a null reference Exception with var type

I am trying to fetch data from CRM using this API.我正在尝试使用此 API 从 CRM 获取数据。 I get an error我收到一个错误

Runtime binding on a null reference null 参考上的运行时绑定

whenever I try to get value from data.fullname .每当我尝试从data.fullname获取价值时。 Is there any way I can fix it?有什么办法可以解决吗?

Thanks谢谢

var response = httpClient.GetAsync("contacts?$select=fullname,emailaddress1").Result;
           
if (response.IsSuccessStatusCode)
{
    var accounts = response.Content.ReadAsStringAsync().Result;

    var jRetrieveResponse = JObject.Parse(accounts);

    dynamic collContacts = JsonConvert.DeserializeObject(jRetrieveResponse.ToString());

    try
    {
        foreach (var data in collContacts.value)
        {
            // You can change as per your need here
            if (data.fullname.Value != null)
            {
                success[i] = data.fullname.Value;
            }

            i ++;
        } 
    }
    catch (Exception)
    { 
         throw; 
    }
}
    

Replace代替

if (data.fullname.Value != null)

with this有了这个

if  (!String.IsNullOrWhiteSpace(data.fullname.Value))

OR Replace或更换

try
{
    foreach (var data in collContacts.value)
    {
        // You can change as per your need here
        if (data.fullname.Value != null)
        {
            success[i] = data.fullname.Value;
        }

        i ++;
    } 
}
catch (Exception)
{ 
     throw; 
}

With

try
{
    foreach (var data in collContacts.value)
    {
        success[i] = data?.fullname?.Value;
        i ++;
    } 
}
catch (Exception)
{ 
     throw; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM