5

I am having trouble with an if statement for checking if an object is null.

I have a webClient go and pull a JSON string from a website in a try/catch. If it errors, it is because the 3 digit country does not exist in the API and I just want to skip it.

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. This works fine.

However, I am getting an error in the if statement that "use of unassigned class 'ro'.

Can someone help me fix this? ro works as expected inside the if?

I have also tried checking a specific node and it is still getting hung up on the 'ro' part.

Also, this is a script component in SSIS.

Thanks

KeithL
  • 5,348
  • 3
  • 19
  • 25

4 Answers4

9
  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. 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:

    if (ro != null && ro.Region != null)
    {
        Row.Region = ro.Region;
        Row.SubRegion = ro.Subregion;
    }
    
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Thanks. Solving this was really #1. I also changed the if to just if(ro!=null) and achieved desired results. – KeithL Feb 15 '19 at 15:03
6

Give ro a default value, so change RootObject ro to RootObject ro = null;

and change ro.Region != null to ro?.Region != null to allow for ro being 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;
}
maxime bélair
  • 154
  • 1
  • 5
Murray Foxcroft
  • 12,785
  • 7
  • 58
  • 86
4

The object ro should be initialized ,cause it is called outside the try scope

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;
}
gandalf
  • 451
  • 5
  • 18
3

Initialize ro with RootObject ro = null; and change the if statement to

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

Aars93
  • 379
  • 4
  • 10