[英]Why does setting a static object cause my method calls to abort?
I have a WPF application which contains 4 tabs. 我有一个包含4个选项卡的WPF应用程序。 Tab1 is the first tab loaded with the application. Tab1是应用程序加载的第一个选项卡。 I have a Tab1Load method (the name of the tab) which looks like: 我有一个Tab1Load方法(选项卡的名称),看起来像:
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 myConfig在Window1.xaml.cs文件的顶部声明
LoadConfigData myConfig = new LoadConfigData();
LoadConfigData looks like: LoadConfigData看起来像:
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. 第一个方法调用LoadGen2Data一直运行到最后,这时它将为静态对象设置一个值。 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: Gen2声明为:
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). 在调用Gen2.ApiKeys.Add之后,应用程序会跳出其所在的方法(LoadGen2Data),而不会调用其余的Add语句,然后退出LoadConfigurationData方法,而不会调用其余的两个方法(LoadXiphosData和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). 进入foreach循环的顶部,我已经验证了是否填充了“名称”值,并且APIKeys [count]提供了合法值(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). 没有其他调试点被捕获,我可以在应用程序GUI周围移动而不会出现问题(也没有数据)。
Any idea what the problem is? 知道是什么问题吗?
Thanks, 谢谢,
Jason 杰森
You are hitting a NullReferenceException
in: 您在以下位置遇到NullReferenceException
:
public static Dictionary<string, string> LongNames { get; set; }
because the auto-generated backing property is null
. 因为自动生成的backing属性为null
。 This also happens for the other Dictionary
variables and the List
variable. 其他Dictionary
变量和List
变量也会发生这种情况。
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; } }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.