Nullable object must have a value?

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

Nullable object must have a value

and i am not sure why. All I want to do is get the value in the database of travel which is currently false. Any help would be appreciated.

using (var db = new DataClasses1DataContext())
{
    var fill = (from f in db.expenseHdrs
                where f.rptNo == getPkRowReport()
                select f).FirstOrDefault();

    txtReportDesc.Text = fill.description;
    txtPeriod.Text = fill.period;
    txtPurpose.Text = fill.purpose;

    bool travel = fill.travel.Value;
    chkTravel.Checked = travel 
 }

You can always switch to

 fill.travel.GetValueOrDefault()

To provide the default (false), or the value of the boolean column from the database. Or you can specify the default with an overload. Either way, the nullable currently doesnt have a value, which is why you get that exception.

System.InvalidOperationException: Nullable object must have a value. The error highlights "lldt" line, instead of "dcdt" which comes first. That is strange in and of itself. Both these fields in the database "allow nulls" is checked. And they both could be null or neither might be null.

You are trying to access property from the nonexistent object (your fill.travel is null, and you calling prop from it), you can use coalesce operator (.Net 4.0):

bool travel = fill.travel ?? false;

Nullable object must have a value. myNewDT.MyDateTime.Value - is valid and contain a regular DateTime object. What is the meaning of this message and what am I doing wrong? Note that oldDTE is not null. I've removed the Value from myNewDT.MyDateTime but the same exception is thrown due to a generated setter.

You will get a InvalidOperationException if you access the Nullable.Value property when the HasValue property is false.

Nullable object must have a value. myNewDT.MyDateTime.Value – is valid and contain a regular DateTime object. What is the meaning of this message and what am I doing wrong? Note that oldDTE is not null.

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;
}

Experincing issues on creating a custom table, Have the following frown (error) message on executing the code below. Error Message: Nullable Object must have a value.

The value coming from the database is a nullable boolean. When you call Nullable.Value, and the value is null, you will get this exception. Consider checking the property Nullable.HasValue before calling .Value.

A nullable type has the same assignment rules as a value type. It must be assigned a value before using it if nullable types are declared in a function as local variables. If it is a field of any class then it will have a null value by default. For example, the following nullable of int type is declared and used without assigning any value.

A nullable value type T? represents all values of its underlying value type T and an additional null value. For example, you can assign any of the following three values to a bool? variable: true, false, or null. An underlying value type T cannot be a nullable value type itself.

To retrieve the value of a variable of a nullable type, you should first test its HasValue property to confirm that it has a value. If you try to read the value when HasValue is False, Visual Basic throws an InvalidOperationException exception.

Comments
  • what does getPkRowReport return?
  • It sounds like fill.travel is a nullable bool (bool?), and it is NULL in the database.
  • @driis, that's the answer, so you might as well move it down below.
  • If you want accurate help, provide the line that causes the error.
  • @DanielA.White It returns the pk of the selected record on the grid.
  • this right here was it. tried to cast a bool from a bool? on a class that didn't exist. Starting at me, right under my nose.