C# “神秘的”;对象引用未设置为对象的实例;

C# “神秘的”;对象引用未设置为对象的实例;,c#,C#,我理解这个错误的意思,但很奇怪我想不出任何原因, 以下是我的简化结构: public partial class mainForm : Form { public mainForm() { InitializeComponent(); IgnoreList = new SortedSet<string>(); IgnoreListQueue IgnoreQueue = new IgnoreListQueue();

我理解这个错误的意思,但很奇怪我想不出任何原因, 以下是我的简化结构:

 public partial class mainForm : Form
{
    public mainForm()
    {
        InitializeComponent();
        IgnoreList = new SortedSet<string>();
        IgnoreListQueue IgnoreQueue = new IgnoreListQueue();
    }
    public class IgnoreListQueue
    {
        private Dictionary<string, int> myQueue;
        public void Add(string s)
        {
        }
        public IgnoreListQueue()
        {
            myQueue = new Dictionary<string, int>();
        }
        public bool contains()
        {}
        ~IgnoreListQueue()
        {
        }
    }
    public SortedSet<string> IgnoreList;
    public IgnoreListQueue IgnoreQueue;
    public int LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
    {
        //IgnoreList is fine here
        //IgnoreQueue is null here.
                    //Example:
                    // bool boo = IgnoreQueue.contains(some string);
    }
}
public分部类mainForm:Form
{
公共表格(
{
初始化组件();
IgnoreList=新分拣数据集();
IgnoreListQueue IgnoreQueue=新IgnoreListQueue();
}
公共类IgnoreListQueue
{
私有字典队列;
公共无效添加(字符串s)
{
}
公共IgnoreListQueue()
{
myQueue=新字典();
}
公共布尔包含()
{}
~IgnoreListQueue()
{
}
}
公共分类数据集信号列表;
公共IgnoreListQueue IgnoreQueue;
公共int低层键盘PROC(int nCode、IntPtr wParam、IntPtr lParam)
{
//无知者在这里很好
//IgnoreQueue在此为空。
//例如:
//bool boo=IgnoreQueue.contains(一些字符串);
}
}
在LowLevelKeyboardProc()函数中,IgnoreQueue被视为null,当VS调试崩溃时,它实际显示IgnoreQueue为null指针。
由于我的程序钩住了键盘笔划,所以我无法在LowLevelKeyboardProc()函数中设置断点。但是,我能够在mainForm()构造函数中设置断点,并显示IgnoreQueue确实已初始化,并在该点加载了一些数据。

有什么想法吗?

这只是一个范围问题。IgnoreQueue在mainform()构造函数中声明,在LowLevel KeyboardProc()方法中不可用。但是,IgnoreList在全局级别声明,并在构造函数中初始化。

这只是一个范围问题。IgnoreQueue在mainform()构造函数中声明,在LowLevel KeyboardProc()方法中不可用。但是,IgnoreList在全局级别声明,并在构造函数中初始化。

public mainForm()
 public mainForm()
    {
        InitializeComponent();
        IgnoreList = new SortedSet<string>();
        IgnoreQueue = new IgnoreListQueue(); //no new declaration, this will set the public IgnoreListQueue you define later.
    }
{ 初始化组件(); IgnoreList=新分拣数据集(); IgnoreQueue=new IgnoreListQueue();//无新声明,这将设置您稍后定义的公共IgnoreListQueue。 }
不过,建议在构造函数之前声明类字段,以提高可读性。还应该考虑让它们<代码>私有< /代码>,你应该< <强>永远> /强>声明实例变量为<代码>公共< /代码> .< /p> <代码>公共主机() { 初始化组件(); IgnoreList=新分拣数据集(); IgnoreQueue=new IgnoreListQueue();//无新声明,这将设置您稍后定义的公共IgnoreListQueue。 } 不过,建议在构造函数之前声明类字段,以提高可读性。还应该考虑让它们<代码>私有< /代码>,你应该< <强>永远> /强>声明实例变量为<代码>公共< /代码> .< /p> < p>此行:

IgnoreList = new SortedSet<string>();
声明并初始化名为
IgnoreQueue
的局部变量,该变量与同名的成员字段不同。在此之后,成员字段仍将为
null

由于您实际上希望初始化其他成员字段,因此需要执行以下操作:

IgnoreQueue = new IgnoreListQueue();
这一行:

IgnoreList = new SortedSet<string>();
声明并初始化名为
IgnoreQueue
的局部变量,该变量与同名的成员字段不同。在此之后,成员字段仍将为
null

由于您实际上希望初始化其他成员字段,因此需要执行以下操作:

IgnoreQueue = new IgnoreListQueue();

问题是您实际上并没有初始化mainForm类的成员变量“IgnoreQueue”,而是在构造函数中创建IgnoreListQueue的本地实例,其他成员函数将无法使用该实例

理想情况下,构造函数应该是这样的

 public mainForm()
    {
        InitializeComponent();
        this.IgnoreList = new SortedSet<string>();
        this.IgnoreQueue = new IgnoreListQueue();
    }
public mainForm()
{
初始化组件();
this.IgnoreList=新的SortedSet();
this.IgnoreQueue=新IgnoreListQueue();
}

问题在于,您实际上并没有初始化mainForm类的成员变量“IgnoreQueue”,而是在构造函数中创建IgnoreListQueue的本地实例,其他成员函数将无法使用该实例

理想情况下,构造函数应该是这样的

 public mainForm()
    {
        InitializeComponent();
        this.IgnoreList = new SortedSet<string>();
        this.IgnoreQueue = new IgnoreListQueue();
    }
public mainForm()
{
初始化组件();
this.IgnoreList=新的SortedSet();
this.IgnoreQueue=新IgnoreListQueue();
}

您的问题取决于变量的范围,并且与IgnoreQueue相关。让我们看一下代码,看看发生了什么:

public IgnoreListQueue IgnoreQueue;
public mainForm()  
{
    InitializeComponent();
    IgnoreList = new SortedSet<string>();
    IgnoreListQueue IgnoreQueue = new IgnoreListQueue();
}
但是,更好的方法是使用,this关键字,这将帮助您立即发现问题

public IgnoreListQueue IgnoreQueue;
public mainForm()  
{
    this.InitializeComponent();
    this.IgnoreList = new SortedSet<string>();
    this.IgnoreQueue = new IgnoreListQueue();
}
公共IgnoreListQueue IgnoreQueue;
公共表格(
{
this.InitializeComponent();
this.IgnoreList=新的SortedSet();
this.IgnoreQueue=新IgnoreListQueue();
}

您的问题取决于变量的范围,并且与IgnoreQueue相关。让我们看一下代码,看看发生了什么:

public IgnoreListQueue IgnoreQueue;
public mainForm()  
{
    InitializeComponent();
    IgnoreList = new SortedSet<string>();
    IgnoreListQueue IgnoreQueue = new IgnoreListQueue();
}
但是,更好的方法是使用,this关键字,这将帮助您立即发现问题

public IgnoreListQueue IgnoreQueue;
public mainForm()  
{
    this.InitializeComponent();
    this.IgnoreList = new SortedSet<string>();
    this.IgnoreQueue = new IgnoreListQueue();
}
公共IgnoreListQueue IgnoreQueue;
公共表格(
{
this.InitializeComponent();
this.IgnoreList=新的SortedSet();
this.IgnoreQueue=新IgnoreListQueue();
}

此代码甚至无法编译。此表单中是否有存储实例的字段?您发布的代码中的
IgnoreQueue
变量是一个局部变量,因此您当然不能在构造函数之外访问它。。。是否还有一个名为
IgnoreQueue
的成员字段,您在示例中省略了该字段,并将其隐藏在构造函数中?我忘了添加上面提到的两个变量都是mainForm的公共变量。所有代码都编译得很好,并弹出ou