使用 GTK# 和 Mono (C#) 分配变量时出错

Error upon variable assignment using GTK# and Mono (C#)

public void OnCombobox1Changed (object sender, EventArgs e)
{
    handler.name = combobox1.ActiveText;
}

代码示例是一个处理程序(由 gtk# 使用 Mono 的 gui 开发人员自动生成),其中包含我自己的变量分配。我有三个 classes,一个包含我的 gui 组件的 win class,一个数据库处理程序 class,以及我的主要 class。处理程序已经在 main 中实例化并且可以正常工作,我可以使用处理程序从我的数据库中成功填充 combobox1。我可以在上面的信号处理程序中放置一个简单的 Console.Writeline(combobox1.ActiveText); 而不是那里的东西,它工作得很好。我不明白为什么这个变量赋值不起作用,handler.name 是一个 class 级别的字符串变量并且 combobox1.ActiveText 也是一个字符串,所以变量类型不是问题。当我 运行 使用上面的代码而不是 Console.Writeline(combobox1.ActiveText); 时,一旦我 select 来自我的 combobox1:

的字符串,我就会收到以下错误

Exception in Gtk# callback delegate Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object at MainWindow.OnCombobox1Changed (System.Object sender, System.EventArgs e) [0x00000] in /home/slothofdoom/Desktop/Programming/Tracker/Tracker/MainWindow.cs:63 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in /build/mono-5.2.0.224/mcs/class/corlib/System.Reflection/MonoMethod.cs:305 --- End of inner exception stack trace --- at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00043] in /build/mono-5.2.0.224/mcs/class/corlib/System.Reflection/MonoMethod.cs:313 at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /build/mono-5.2.0.224/mcs/class/referencesource/mscorlib/system/reflection/methodbase.cs:229 at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x000e1] in /build/mono-5.2.0.224/mcs/class/corlib/System/Delegate.cs:461 at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00008] in /build/mono-5.2.0.224/mcs/class/corlib/System/MulticastDelegate.cs:67 at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in /build/mono-5.2.0.224/mcs/class/corlib/System/Delegate.cs:406 at GLib.Signal.ClosureInvokedCB (System.Object o, GLib.ClosureInvokedArgs args) [0x0007d] in <940f51fcd0434c359bfa4fdc8e64ad03>:0 at GLib.Signal+SignalClosure.Invoke (GLib.ClosureInvokedArgs args) [0x0000c] in <940f51fcd0434c359bfa4fdc8e64ad03>:0 at GLib.Signal+SignalClosure.MarshalCallback (System.IntPtr raw_closure, GLib.Value* return_val, System.UInt32 n_param_vals, GLib.Value* param_values, System.IntPtr invocation_hint, System.IntPtr marshal_data) [0x00050] in <940f51fcd0434c359bfa4fdc8e64ad03>:0 at GLib.ExceptionManager.RaiseUnhandledException (System.Exception e, System.Boolean is_terminal) [0x00000] in <940f51fcd0434c359bfa4fdc8e64ad03>:0 at GLib.Signal+SignalClosure.MarshalCallback (System.IntPtr raw_closure, GLib.Value* return_val, System.UInt32 n_param_vals, GLib.Value* param_values, System.IntPtr invocation_hint, System.IntPtr marshal_data) [0x00000] in <940f51fcd0434c359bfa4fdc8e64ad03>:0 at Gtk.Application.gtk_main () [0x00000] in :0 at Gtk.Application.Run () [0x00000] in :0 at Tracker.MainClass.Main (System.String[] args) [0x0002e] in /home/slothofdoom/Desktop/Programming/Tracker/Tracker/Program.cs:54

如果能帮助理解如何解决这个问题,我们将不胜感激。

编辑:我之前清除过很多NullReferenceExceptions,一般都是范围错误,但是这次求助是因为我不明白我怎么会超出范围,也是值得一提的是,当我尝试编译时,我通常会得到 NullReferenceException,并且它没有所有那些油嘴滑舌的警告和东西。

Edit2:在尝试调试它的过程中,我尝试将 name 变量设置为与我的信号处理程序相同的 class,并且它工作正常。我还尝试在我的信号处理程序所在的 win class 中设置设置器以在我的数据库处理程序 class 中设置 handler.name 并且它抛出完全相同的错误,就像我只得到当我尝试将 combobox1.ActiveText 中的字符串分配给另一个 class 中的变量时出现的错误,或者如果我将它分配给 win class 中的变量时出现的错误如果我尝试将该变量传递给另一个 class 以进行任何类型的操作

我使用与 MonoDevelop 捆绑在一起的 Gtk# 设计器创建了一个尽可能接近您描述的情况的程序。这里的关键项目是 OnComboBox1Changed()combobox1checkbox1

combobox1初始化为三个选项,即option1、option2和option2。

public MainWindow() : base(Gtk.WindowType.Toplevel)
{
    string defaultText = "Make your choice";

    Build();

    this.combobox1.InsertText( 0, "option1" );
    this.combobox1.InsertText( 1, "option2" );
    this.combobox1.InsertText( 2, "option3" );

    this.Title = defaultText;
    this.label1.Text = defaultText;
}

然后我们有ComboBox1Changed():

protected void OnComboBox1Changed(object sender, EventArgs e)
{
    string text = combobox1.ActiveText;

    Console.WriteLine( text );
    this.Title = "Chosen: " + text; 
    label1.Text = text;
}

问题是我无法使用此代码使应用程序崩溃。现在,假设 label1 是您的 handler,而 Text 是您的 Name 属性 在 处理程序中 .

我添加了一个复选框,checkbox1,您可以在上图中看到。这个想法是,当用户取消选中它时,label1 设置为 null,这使 label1.Text = text 处于相同的情况我怀疑你的代码中有handler.Name = ActiveText.

protected void OnCheckBox1Toggled(object sender, EventArgs e)
{
    if ( !this.checkbutton1.Active ) {
        this.label1 = null;
    }
}

...然后我们得到了预期的结果:应用程序在 OnComboBox1Changed() 中崩溃,出现与您报告的相同的异常。对我来说,这表明问题不在 combobox1ActiveText 属性 的 ComboBox,但在您的数据库处理程序中,出于某种原因,它必须是 null

希望对您有所帮助。