6

I have a null error on my DTO object at runtime:

enter image description here

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?

1
  • 1
    I know this comment is off-topic, but bravo for asking a clear, detailed, answerable question. I see so many terrible questions on here, this is how it should be done. Commented Apr 12, 2012 at 14:28

3 Answers 3

6

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
Sign up to request clarification or add additional context in comments.

Comments

3

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?

Comments

1

You need to cast to (CompanyColumnsEnumDTO?) instead of (CompanyColumnsEnumDTO)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.