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

The code sample is a handler (auto generated by gtk# using Mono's gui developer) with my own variable assignment in it. I have three classes, a win class that contains my gui components, which the sample is from, a database handler class, and my main class. Handler is already instantiated in main and works with no problems, I can successfully populate combobox1 from my database using handler. I can put a simple Console.Writeline(combobox1.ActiveText); in the signal handler above instead of what's there and it works perfectly. I cannot figure out why this variable assignment doesn't work, handler.name is a class level string variable and combobox1.ActiveText is a string as well, so variable type isn't the problem. When I run this with the above code instead of the Console.Writeline(combobox1.ActiveText); I get the following error as soon as I select a string from my 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

Any help with understanding how to fix this would be very appreciated.

Edit: I have cleared many NullReferenceExceptions before, normally it turns out to be due to an error in scope, but this time I am asking for help because I do not understand how I could be out of scope, it's also worth mentioning I normally get the NullReferenceException when I try to compile, and it doesn't have all those glib warnings and stuff.

Edit2: It may also be relevant to say that in the process of attempting to debug this I tried making the name variable in the same class as my signal handler, and it worked fine. I have also tried to make setters in the win class where my signal handler is located to set handler.name in my database handler class and it throws the exact same error, it's like I only get the error when I try to assign the string in combobox1.ActiveText to a variable in another class, or if I assign it to a variable in win class I also get the error if I try to pass that variable to another class for manipulation of any kind

SolidDave
  • 13
  • 5
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – thehennyy Feb 01 '18 at 20:15
  • Combos ActiveText may be null. If the combo is not an editable combo I believe you have to find the text value some other way, ActiveText does not get populated. Could this be it? I think in the past I’ve had to keep a separate array of the combo values and use combo.active to look up the selected value. – muszeo Feb 02 '18 at 03:51
  • Hey muszeo, thanks for the idea, I will look into the array of combo values method, but I know for certain that my combo is not null, like I said in my original post, I can run Console.Writeline(combobox1.ActiveText); inside my signal handler and it works. ActiveText is supposed to be a get property for combo's active string. – SolidDave Feb 02 '18 at 08:03
  • It seems then that ActiveText is not the source of the error, the remaining candidate is handler. Can this be null? You said that Console.WriteLine(combobox1.ActiveText) works perfectly, an instance in which the suspect does not appear. – Baltasarq Feb 02 '18 at 20:17

1 Answers1

0

I've created a program as close as possible to the situation you describe, using the Gtk# designer bundled with MonoDevelop. The key items here are the OnComboBox1Changed(), combobox1, and checkbox1.

The demo application

The combobox1 is initialized to have three options, namely option1, option2 and 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;
}

Then we have ComboBox1Changed():

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

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

The thing is that I haven't been able to make the app crash with this code. Now, let's say that label1 is your handler, and Text is your Name property in handler.

I have added a check box, checkbox1, which you can see in the picture above. The idea is that, when the user unchecks it, label1 is set to null, which puts the label1.Text = text in the same situation I suspect you have in your code with handler.Name = ActiveText.

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

...and then we obtain the expected result: the app crashes in OnComboBox1Changed() with the same exception you have reported. For me this demonstrates that the problem is not in the combobox1 or in the ActiveText property of ComboBox, but in your database handler, which must be null for some reason.

Hope this helps.

Baltasarq
  • 12,014
  • 3
  • 38
  • 57
  • 1
    Hey Baltasarq, I'vebeen looking into my handler being null for the past few days and found I had two instances of two different handlers (both named handler) in two different classes. I managed to solve most of my problem by just instantiating a new handler object for the GUI class and the error went away. I then went further with my OncomboboxChanged handler and and am now getting a different error that I think is unrelated (which I think I can solve). Thank you for your help, especially for this clear and concise example of how I should be debugging a situation like this when it comes up. – SolidDave Feb 06 '18 at 01:06
  • Thanks. You can choose my answer to be the correct one if you think it helped you. – Baltasarq Feb 06 '18 at 10:58