Nullable boolean property throws exception

Dec 11, 2013 at 12:30 PM
I get NullReferenceException when I try to add new record (it surfaces in ObjectEditControl.ascx).

This seems to be related to one of the properties of that class (mapped to db table with EF5.0 code first).

It looks something like this:
public virtual bool? SomeProperty { get; set; }
When it is declared like that:
public virtual bool SomeProperty { get; set; }
it works (though it's incorrect because in the database it is nullable column...).

So the question: is it possible to have nullable booleans as properties or for some reasons it is not supported by the framework?
Coordinator
Dec 11, 2013 at 1:23 PM
Adam

At present, Naked Objects Mvc does not support nullable booleans (see https://nakedobjects.codeplex.com/workitem/49 ). This is something we are going to fix.

In the meantime - if your existing Db schema requires that it be a nullable boolean - you could leave the property as bool? but mark it up as [Hidden] (or even [NakedObjectsIgnore]) and then render it as a derived property e.g.:
[DisplayName("Some Property")]
public virtual bool SomePropertyAsBoolean {
  get {
       return SomeProperty == null ? false : SomeProperty.Value; 
  }
  set  {
      SomeProperty = value;
 }
}

[NakedObjectsIgnore]
public virtual bool? SomeProperty { get; set; }
or you could make the derived property be of an enum type with values True, False and Null.
Marked as answer by Adam_G on 12/11/2013 at 8:34 AM
Dec 11, 2013 at 3:34 PM
Hi Richard,

Great to know that it's going to be fixed. Thanks for the workaround (the one with enum I think is better - it allows to set column to null at any given time).