简体   繁体   English

为什么设置静态对象会导致我的方法调用中止?

[英]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.

相关问题 为什么我的静态方法隐藏了我的实例方法? - Why does my Static method hide my instance method? 为什么设置 static 方法会导致堆栈溢出? - Why does setting a static method result in a stack overflow? 为什么在DataContext上设置Log会导致ObjectDisposedException? - Why does setting Log on a DataContext cause an ObjectDisposedException? 为什么 C# 编译器不会在静态方法调用实例方法的地方出现错误代码? - Why does the C# compiler not fault code where a static method calls an instance method? 为什么使 Application_Start 成为 static 方法(FxCopAnalyzer 的推荐)会导致 404 错误? - Why does making Application_Start a static method (a recommendation of FxCopAnalyzer) cause 404 errors? 为什么我的代码会导致死锁? - Why does my code cause a deadlock? 为什么我的应用程序会导致SmartScreen消息? - Why does my application cause a SmartScreen message? 为什么OleFlushClipboard会使我的存储对象丢失其类ID? - Why does OleFlushClipboard cause my storage object to lose its class id? 为什么使用我的静态属性在XAML中设置字体大小不起作用? - Why setting the font size in XAML using my static property does not work? 为什么将ComboBox.SelectedValue设置为null会导致ArgumentNullException? - Why does setting ComboBox.SelectedValue to null cause a ArgumentNullException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM