I have a null error on my DTO object at runtime:
I didn't understand because column is nullable:
[DataContract]
public class SearchParametersCompanyDTO
{
public SearchParametersCompanyDTO();
[DataMember]
public CompanyColumnsEnumDTO? Column { get; set; }
[DataMember]
public int PageIndex { get; set; }
[DataMember]
public int PageSize { get; set; }
[DataMember]
public string Term { get; set; }
}
[DataContract]
public enum CompanyColumnsEnumDTO
{
[EnumMember]
CompanyName = 0,
[EnumMember]
City = 1,
[EnumMember]
PostCode = 2,
}
It must be a conversion problem because null is accepted on Column:
var dto = new SearchParametersCompanyDTO
{
PageIndex = pageIndex,
PageSize = defaultPageSize,
Term = term,
Column = null
};
Any idea?
You're trying to cast a null value to an enum type (rather than a nullable enum type). I'm guessing you actually want to change your cast to:
Column = (CompanyColumnsEnumDTO?) column
The problem here is you're casting the value column
into a non-nullable value CompanyColumnsEnumDTO
. Based on the exception it looks like column
is null
here and casting to a non-null appropriately throws an exception. Did you mean to cast to CompanyColumnsEnumDTO?
instead?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With