How to skip to next item if current variable is null

I have a function that retrieves a list of device names and stores then in a variable. Then the next step is to get info using 1 device name per line and keep going till the loop is complete.

 String text = "";
        String errors = "";

    for (int i = 0; i < collection.Result.Count; i++)
    {
        deviceNames += collection.Result[i].DeviceName + Environment.NewLine;

        getvirtuals.Location = deviceNames;
        var virtuals = client.GetKnownVirtuals(getvirtuals, LtmKeyType.VirtualAddressPort);

        if (virtuals.Result == null)
        {
            i++;
            getvirtuals.Location = deviceNames;

        for (int v = 0; v < virtuals.Result.Count; v++)
                {
                try
                {
                    LtmKey virtualKey = new LtmKey();
                    virtualKey.Location = virtuals.Result[v].Location;
                    virtualKey.LocationType = virtuals.Result[v].LocationType;
                    virtualKey.Key = virtuals.Result[v].Key;
                    virtualKey.KeyType = LtmKeyType.VirtualAddressPort;

                    virtualKey.AdminGroup = admingroupComboBox.Text;


                    var memberStatus = client.GetMemberStatus(virtualKey);


                    for (int j = 0; j < memberStatus.Result.Count; j++)
                    {
                        VirtualMemberStatus status = memberStatus.Result[j];
                        text += String.Format("{5},{4},{0},{1},{2},{3}" + Environment.NewLine, status.Member.Address, status.Member.Port, status.EffectiveStatus, status.DesiredStatus, virtualKey.Key.Replace(":", ","), DateTime.UtcNow);
                        toolStripProgressBar1.PerformStep();
                    }
                }
                catch
                {
                    errors += String.Format("{0} Error Code: 2, Error occurred, check device name (case senstive) and admin group. This error may also occur due to connection loss, try again." + Environment.NewLine, DateTime.UtcNow);
                }
            }
            this.allResultsBox.Text = text;

        getallstatusButton.Enabled = true;
    }

    }

The problem that I am running into is that if virtuals is null the tool crashes, instead what I want to do is if virtuals = null I want to move onto the next item from the list. I have tried a if statement but it is not working the way planned, it still comes back as null.

Answers


Well this seems like a problem to start with:

if (virtuals.Result == null)
{
    i++;
    getvirtuals.Location = deviceNames;

    for (int v = 0; v < virtuals.Result.Count; v++)

    ...

If virtuals.Result is null, how do you expect virtuals.Result.Count to work? I suspect you meant:

if (virtuals.Result != null)

However, I suspect you really just want:

// Keep going with the next iteration of the for loop
if (virtuals == null || virtuals.Results == null)
{
    continue;
}

Need Your Help

Python unicode encoding using UTF-8

python unicode utf-8 character-encoding

I was following through python's tutorial on unicode and I've got a simple question to ask:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.