元组<列表<字符串>,列表<字符串>&GT;调用.AddRange时给出NullReferenceException

时间:2016-04-13 16:36:29

标签: c# .net azure

我正在尝试将config.ContainerNamescontainer添加到public Tuple<List<string>,List<string>> ListContainerData {get; internal set;}。但是我遇到了NullReferenceException

ListContainer()

public static Tuple<List<string>,List<string>> ListContainer(StartConfig config)

    {

        if (config != null || config.BlobClient != null)
        {
            config = Program.GetConfig();
        }
        if (config == null)
        {
            throw new ArgumentNullException("config");
        }
        if (config.BlobClient == null)
        {
            throw new ArgumentException("BlobClient must not be null", "config");
        }
        List<string> container = new List<string>();

        //Get the list of the blob from the above container
        IEnumerable<CloudBlobContainer> containers = config.BlobClient.ListContainers();


        foreach (CloudBlobContainer item in containers)
        {
            container.Add(item.Name);

            config.ContainerNames.Add(String.Join("\n", container));

        }

        config.ListContainerData.Item2.AddRange(config.ContainerNames);
        config.ListContainerData.Item1.AddRange(container);

        //Adding a print statement

        Console.WriteLine(String.Join("\n", container));
        Console.WriteLine("\n");

        // Console.WriteLine(startConfig.ContainerNames.ToString());

        return config.ListContainerData;


    }

StartConfig klass

public class StartConfig
{
    public StartConfig()
    {
        ContainerNames = new List<string>();
    }

    public CloudStorageAccount StorageAccount { get; internal set; }
    public CloudBlobClient BlobClient { get; internal set; }
    public CloudBlobContainer Container { get; internal set; }

    public List<string> ContainerNames { get; internal set; }

    public Tuple<List<string>,List<string>> ListContainerData { get; internal set; }

}

我不确定为什么config.ListContainerData.Item1.AddRange(container);&amp;&amp; config.ListContainerData.Item2.AddRange(config.ContainerNames);都抛出Null异常,有人可以解释为什么会发生这种情况以及我如何解决Null异常?谢谢!

1 个答案:

答案 0 :(得分:2)

在添加之前,您需要创建ListContanierData和这两个列表:

public static Tuple<List<string>,List<string>> ListContainer(StartConfig config)

{

    if (config != null || config.BlobClient != null)
    {
        config = Program.GetConfig();
    }
    if (config == null)
    {
        throw new ArgumentNullException("config");
    }
    if (config.BlobClient == null)
    {
        throw new ArgumentException("BlobClient must not be null", "config");
    }
    List<string> container = new List<string>();

    //Get the list of the blob from the above container
    IEnumerable<CloudBlobContainer> containers = config.BlobClient.ListContainers();


    foreach (CloudBlobContainer item in containers)
    {
        container.Add(item.Name);

        config.ContainerNames.Add(String.Join("\n", container));

    }

    config.ListContainerData = new Tuple<List<string>, List<string>>(new List<string>(), new List<string>());
    //or
    //config.ListContainerData = Tuple.Create(new List<string>(), new List<string>());
    config.ListContainerData.Item2.AddRange(config.ContainerNames);
    config.ListContainerData.Item1.AddRange(container);

    //Adding a print statement

    Console.WriteLine(String.Join("\n", container));
    Console.WriteLine("\n");

    // Console.WriteLine(startConfig.ContainerNames.ToString());

    return config.ListContainerData;


}