开发者

Why does setting a static object cause my method calls to abort?

I have a WPF application which contains 4 tabs. Tab1 is the first tab loaded with the appl开发者_运维技巧ication. I have a Tab1Load method (the name of the tab) which looks like:

private void Tab1Load(object sender, RoutedEventArgs e)
    {
        myConfig.LoadConfigurationData();
        XiphosDB.DataContext = Xiphos.XiphosDbNames;
    }

myConfig was declared at the top of the Window1.xaml.cs file

LoadConfigData myConfig = new LoadConfigData();

LoadConfigData looks like:

public void LoadConfigurationData()
    {
        LoadGen2Data();
        LoadXiphosData();
        LoadTestConsumerData();
    }

The first method call, LoadGen2Data, runs all the way to the end at which point it sets a value to static object. The call is:

var count = 0;
        foreach (var name in Gen2.allFiNames)
        {
            Gen2.ApiKeys.Add(name, APIKeys[count]);
            Gen2.ConnectStrings.Add(name, connectStrings[count]);
            Gen2.LongNames.Add(name, LongNames[count]);
            count++;
        }

The Gen2 declaration is:

 public class Gen2
{
    public static List<string> allFiNames { get; set; }

    public static Dictionary<string, string> LongNames { get; set; }
    public static Dictionary<string, string> ApiKeys { get; set; }
    public static Dictionary<string, string> ConnectStrings { get; set; }
}

Right after Gen2.ApiKeys.Add is called, the application skips out of the method it is in (LoadGen2Data) without calling the rest of the Add statements and then exits out of the LoadConfigurationData method without calling the remaining two methods (LoadXiphosData and LoadTestConsumerData).

Stepping into the top of the foreach loop I have verified that the "name" value is populated and APIKeys[count] provides a legitimate value (a GUID).

I do not receive any errors. No other debug points are caught and I can move around the application GUI with no problems (and no data).

Any idea what the problem is?

Thanks,

Jason


You are hitting a NullReferenceException in:

public static Dictionary<string, string> LongNames { get; set; }

because the auto-generated backing property is null. This also happens for the other Dictionary variables and the List variable.

You can fix it like this:

private static Dictionary<string, string> longNames = new Dictionary<string, string>();

public static Dictionary<string, string> LongNames { get { return longNames; } set { longNames = value; } }

You may even skip the setter in this case.

public class Gen2
{
    private static List<string> allFiNames = new List<string> allFiNames();
    public static List<string> AllFiNames { get { return allFiNames; } }

    private static Dictionary<string, string> longNames = new Dictionary<string, string>();
    public static Dictionary<string, string> LongNames { get { return longNames; } }

    private static Dictionary<string, string> apiKeys = new Dictionary<string, string>();
    public static Dictionary<string, string> ApiKeys { get { return apiKeys; } }

    private static Dictionary<string, string> connectStrings = Dictionary<string, string>();
    public static Dictionary<string, string> ConnectStrings { get { return connectStrings; } }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜