[英]Error upon variable assignment using GTK# and Mono (C#)
public void OnCombobox1Changed (object sender, EventArgs e)
{
handler.name = combobox1.ActiveText;
}
该代码示例是一个处理程序(由我使用自己的变量赋值的gtk#使用Mono的gui开发人员自动生成)。 我有三个类,一个包含我的gui组件(该示例来自该组件)的win类,一个数据库处理程序类和一个主类。 处理程序已在main中实例化,并且可以正常使用,我可以使用处理程序从数据库成功填充combobox1
。 我可以放一个简单的Console.Writeline(combobox1.ActiveText);
在上面的信号处理程序中,而不是在那里,它可以完美地工作。 我无法弄清楚为什么此变量分配不起作用, handler.name
是类级别的字符串变量,而combobox1.ActiveText
也是字符串,因此变量类型不是问题。 当我使用上面的代码而不是Console.Writeline(combobox1.ActiveText);
来运行它时Console.Writeline(combobox1.ActiveText);
我从combobox1
选择一个字符串后,立即收到以下错误消息:
Gtk#回调委托中的异常注意:应用程序可以使用GLib.ExceptionManager.UnhandledException处理异常。 System.Reflection.TargetInvocationException:调用的目标引发了异常。 ---> System.NullReferenceException:对象引用未设置为/ home / slothofdoom / Desktop / Programming / Tracker / Tracker /中MainWindow.OnCombobox1Changed(System.Object发件人,System.EventArgs e)[0x00000]上的对象实例MainWindow.cs:63(包装器托管到本机)System.Reflection.MonoMethod:InternalInvoke(System.Reflection.MonoMethod,object,object [],System.Exception&)在System.Reflection.MonoMethod.Invoke(System.Object obj,System.Reflection.BindingFlags invokeAttr,System.Reflection.Binder绑定程序,System.Object []参数,System.Globalization.CultureInfo文化)[0x00032] /build/mono-5.2.0.224/mcs/class/corlib/System .Reflection / MonoMethod.cs:305 ---内部异常堆栈跟踪的结尾---在System.Reflection.MonoMethod.Invoke(System.Object obj,System.Reflection.BindingFlags invokeAttr,System.Reflection.Binder绑定程序,System。 /build/mono-5.2.0.224/mcs/class/corlib/System.Reflection/中的Object []参数,System.Globalization.CultureInfo文化)[0x00043] 在/build/mono-5.2.0.224/mcs/class/referencesource/mscorlib/system/reflection中的System.Reflection.MethodBase.Invoke上的MonoMethod.cs:313(System.Object obj,System.Object []参数)[0x00000] /methodbase.cs:229在System.Delegate.DynamicInvokeImpl(System.Object [] args)[0x000e1]在/build/mono-5.2.0.224/mcs/class/corlib/System/Delegate.cs:461在System.MulticastDelegate .build / mono-5.2.0.224 / mcs / class / corlib / System / MulticastDelegate.cs:67中位于.System.Delegate.DynamicInvoke(System.Object [] args)的.DynamicInvokeImpl(System.Object [] args)[0x00008] <build / mono-5.2.0.224 / mcs / class / corlib / System / Delegate.cs:406在GLib.Signal.ClosureInvokedCB(System.Object o,GLib.ClosureInvokedArgs args)中的[0x00000]在<940f51fcd0434c359bfa4fdc8e64中在<940f51fcd0434c359bfa4fdc8e64ad03>的<940f51fcd0434c359bfa4fdc8e64ad03>中的GLib.Signal + SignalClosure.Invoke(0x0000c]中为:0:在GLib.Signal + SignalClosure.MarshalCallback(System.IntPtr_val_val,_n_val。 GLib.Value * param_values,System.IntPtr invocation_hint,System.IntPtr marshal_data)[0x00050]在<940f51fcd0434c359bfa4fdc8e64ad03>:0中位于GLib.ExceptionManager.RaiseUnhandledException(System.Exception e,System.Boolean is_terminal)[4xb4fa404351] 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)处为0 [0x00000f4ad4c04f4a4f4c04f4ad43c] / home /中的Tracker.MainClass.Main(System.String [] args)[0x0002e]中的0在Gtk.Application.Run()中的0在Gtk.Application.gtk_main()中的[0x00000]在[0x00000] slothofdoom /台式机/编程/Tracker/Tracker/Program.cs:54
任何了解如何解决此问题的帮助将不胜感激。
编辑:我之前已经清除了许多NullReferenceExceptions,通常结果是由于范围错误引起的,但是这次我寻求帮助是因为我不了解如何超出范围,这也值得一提NullReferenceException,当我尝试编译时,它没有所有这些glib警告和内容。
Edit2:在尝试调试此过程的过程中,我尝试将name变量与信号处理程序放在同一类中,并且效果很好。 我还尝试在信号处理程序所在的win类中设置setter,以在数据库处理程序类中设置handler.name,并且抛出完全相同的错误,就像我尝试在其中分配字符串时仅收到错误一样combobox1.ActiveText到另一个类中的变量,或者如果我将其分配给win类中的变量,如果我尝试将该变量传递给另一个类以进行任何类型的操作,也会收到错误消息
我已经使用与MonoDevelop捆绑在一起的Gtk#设计器创建了一个尽可能接近您所描述情况的程序。 这里的关键项是OnComboBox1Changed()
, combobox1和checkbox1 。
将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是您的处理程序 , Text是您在handler中的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()
崩溃,但报告的异常相同。 对我来说,这表明问题不是在combobox1或ComboBox的ActiveText属性中,而是在您的数据库处理程序中,由于某种原因,该处理程序必须为null
。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.