[Mono-osx] MonoMac - NullReferenceException when binding

JayDZimmerman mono at zimmail.us
Fri Sep 2 21:31:36 EDT 2011


Working from one of the MonoMac samples, I created an application that binds
an NSTextField to a property.  It works as expected until I clear out the
contents of the text field, at which point the follow exception is thrown:

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.[Test.MainWindowController.Void
set_TestStringNotWorking(System.String)]
(MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSString)
<0x0000f>
at (wrapper native-to-managed) object.[Test.MainWindowController.Void
set_TestStringNotWorking(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 Test.MainClass.Main (string[]) <0x00017>


The test case is simple:
- Create a new MonoMac project.
- In the "MainWindowController" class, create the following property:
[Export]
public string TestString { get; set; }
- In the Interface Builder, create NSTextField and bind its Value to the
"File's Owner" object using a Model Key Path of "TestString".
- Run the application, enter a value in the text field, tab away to
validate, clear the value out of the text field, tab away to validate.


If I declare my property to use an NSString instead of a string no exception
is thrown.  This isn't an unreasonable implementation, but it does require
casting to and from the NSString to operate on the value.

This may simply be an unearthing of my ignorance of Cocoa and its "best
practices" implementation (I'm straight out of the Windows .NET world). 
Just looking for someone to point me in the right direction.

--
View this message in context: http://mono.1490590.n4.nabble.com/MonoMac-NullReferenceException-when-binding-tp3787354p3787354.html
Sent from the Mono - OSX mailing list archive at Nabble.com.


More information about the Mono-osx mailing list