简体   繁体   English

C#检查对象是否为空

[英]C# Checking if Object is Null

I am having trouble with an if statement for checking if an object is null. 我遇到了一个if语句,用于检查对象是否为空。

I have a webClient go and pull a JSON string from a website in a try/catch. 我有一个webClient go并从try / catch中的网站中提取JSON字符串。 If it errors, it is because the 3 digit country does not exist in the API and I just want to skip it. 如果它出错,那是因为API中不存在3位数国家,我只是想跳过它。

Here is my code: 这是我的代码:

    System.Net.WebClient wc = new System.Net.WebClient();

    RootObject ro;
    try
    {
        string resp = wc.DownloadString("https://restcountries.eu/rest/v2/alpha/" + Row.Code.ToString());

        JavaScriptSerializer js = new JavaScriptSerializer();

        ro = js.Deserialize<RootObject>(resp);
    }
    catch (Exception e)
    { }

    if (ro.Region != null)
    {
        Row.Region = ro.Region;
        Row.SubRegion = ro.Subregion;
    }

RootObject is a class to deserialize to. RootObject是一个反序列化的类。 This works fine. 这很好用。

However, I am getting an error in the if statement that "use of unassigned class 'ro'. 但是,我在if语句中遇到“使用未分配的类'ro'时出错。

Can someone help me fix this? 有人可以帮我解决这个问题吗? ro works as expected inside the if? ro在if中如预期的那样工作?

I have also tried checking a specific node and it is still getting hung up on the 'ro' part. 我也试过检查一个特定的节点,它仍然挂在'ro'部分。

Also, this is a script component in SSIS. 此外,这是SSIS中的脚本组件。

Thanks 谢谢

  1. It is possible that the try block will never assign a value to ro and thus it will be unassigned outside of try block. try块可能永远不会为ro赋值,因此它将在try块之外被取消分配。 To fix that, assign a value: 要解决此问题,请指定一个值:

     RootObject ro = null; 
  2. Since ro could be null , you need to check for that in your if statement before you try to access a member: 由于ro可能为null ,因此在尝试访问成员之前,需要在if语句中检查:

     if (ro != null && ro.Region != null) { Row.Region = ro.Region; Row.SubRegion = ro.Subregion; } 

Give ro a default value, so change RootObject ro to RootObject ro = null; ro一个默认值,所以将RootObject ro更改为RootObject ro = null;

and change ro.Region != null to ro?.Region != null to allow for ro being null. 并将ro.Region != null更改为ro?.Region != null以允许ro为null。

and possibly do something about swallowing that exception. 并可能做一些关于吞咽这种例外的事情。

System.Net.WebClient wc;

RootObject ro = null;
try
{
    wc = new System.Net.WebClient();
    string resp = wc.DownloadString("https://restcountries.eu/rest/v2/alpha/" + Row.Code.ToString());

    JavaScriptSerializer js = new JavaScriptSerializer();

    ro = js.Deserialize<RootObject>(resp);
}
catch (Exception e)
{
    // Log your error
}

if (ro?.Region != null)
{
    Row.Region = ro.Region;
    Row.SubRegion = ro.Subregion;
}

The object ro should be initialized ,cause it is called outside the try scope 应该初始化对象ro,因为它在try范围之外被调用

RootObject ro = null;
try
{
    string resp = wc.DownloadString("https://restcountries.eu/rest/v2/alpha/" + Row.Code.ToString());

    JavaScriptSerializer js = new JavaScriptSerializer();

    ro = js.Deserialize<RootObject>(resp);
}
catch (Exception e)
{ }

if (ro?.Region != null)
{
    Row.Region = ro.Region;
    Row.SubRegion = ro.Subregion;
}

Initialize ro with RootObject ro = null; RootObject ro = null;初始化ro RootObject ro = null; and change the if statement to 并将if语句更改为

if (ro?.Region != null)
{
    Row.Region = ro.Region;
    Row.SubRegion = ro.Subregion;
}

This way the statement will check if either ro is null or ro.Region is null 这样语句将检查ro是否为null或ro.Region为null

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

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