Bug 1484 - Binding a empty string throws NullReferenceException on NSString.ToString()
Summary: Binding a empty string throws NullReferenceException on NSString.ToString()
Alias: None
Product: MonoMac
Classification: Desktop
Component: Bindings ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2011-10-13 16:04 UTC by loftum
Modified: 2012-11-21 17:07 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description loftum 2011-10-13 16:04:26 UTC
I have this viewmodel:

public class MainWindowViewModel : NSObject
        public string[] AvailableItems { get; private set; }

        public string SelectedItem { get; set; }

        public MainWindowViewModel()
            AvailableItems = new string[] {"MySql", "SqlServer", "Oracle"};
            SelectedItem = AvailableItems.First();

In interface builder I have declared an NSComboBox with its TextField behavior=Editable, and the following binding:

Bind to: File's owner
Model Key Path: model.selectedItem

This works as long as the text in the NSComboBox is not empty.
But if the text is empty I get the following error when NSComboBox loses focus:

[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
at MonoMac.Foundation.NSString.ToString () <0x00009>
at (wrapper dynamic-method) object.[MonoMacBinding.MainWindowViewModel.Void set_SelectedItem(System.String)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSString) <0x0000f>
at (wrapper native-to-managed) object.[MonoMacBinding.MainWindowViewModel.Void set_SelectedItem(System.String)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSString) <0x000fb>
at (wrapper managed-to-native) MonoMac.AppKit.NSApplication.NSApplicationMain (int,string[]) <0x00003>
at MonoMac.AppKit.NSApplication.Main (string[]) <0x00017>
at MonoMacBinding.MainClass.Main (string[]) <0x00017>

It seems like the value from NSComboBox is not string.Empty, bu nil/null.
The binder then fails when calling ToString() on the null value.

Workaround 1: Make the NSComboBox non-editable, and make sure the AvailableItems is not empty.
Workaround 2: Do the binding in code somehow.
Comment 1 Martin Baulig 2012-08-17 03:53:38 UTC
I found the problem, will have a fix for this shortly.
Comment 3 Sebastien Pouliot 2012-11-19 17:37:47 UTC
Martin, any reason to keep it open ?
Comment 4 Martin Baulig 2012-11-21 17:07:39 UTC
No, looks like I simply forgot to close it.