Nullable object must have a value?

后端 未结 8 2365
旧时难觅i
旧时难觅i 2020-12-09 14:54

On the line: bool travel = fill.travel.Value; I am getting the following error:

Nullable object must have a value

a

相关标签:
8条回答
  • 2020-12-09 15:12

    You can check if nullable variable has some value like this before your actually access its value

    if(fill.travel.HasValue)
    {
       bool travel = fill.travel.Value;
    }
    
    0 讨论(0)
  • 2020-12-09 15:19

    I had this happen because there was code like this:

    bool? someVariable = (bool)someNullableBool;
    

    The someNullableBool was null and this error was thrown.

    The solution was to re-write it as:

    bool? someVariable = (bool?)someNullableBool;
    
    0 讨论(0)
提交回复
热议问题