61

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 
 }
9
  • what does getPkRowReport return? Commented May 11, 2012 at 17:06
  • 12
    It sounds like fill.travel is a nullable bool (bool?), and it is NULL in the database. Commented May 11, 2012 at 17:07
  • 1
    @driis, that's the answer, so you might as well move it down below. Commented May 11, 2012 at 17:08
  • If you want accurate help, provide the line that causes the error. Commented May 11, 2012 at 17:09
  • @DanielA.White It returns the pk of the selected record on the grid. Commented May 11, 2012 at 17:50

8 Answers 8

77

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.

Sign up to request clarification or add additional context in comments.

Comments

18

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;

1 Comment

I don't know why this doesn't have more upvotes. It's by far the simplest answer.
7

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

Comments

6

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;

1 Comment

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.
5

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

Comments

4

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.

Comments

1

Null is not false. See Eric Lippert's blog article series about this at: http://blogs.msdn.com/b/ericlippert/archive/2012/03/26/null-is-not-false.aspx

You need to process a null result into a false, if that is how you want to translate it.

To make this case clear, consider this code:

bool travel;
bool? temptravel = fill.travel.Value;
if( temptravel == true )
    travel = true;
else
    travel = false;

Or just use Val Bakhtin's solution if you are using .Net 4

Comments

1

You can always use the classic C ? operator like this:

bool travel = (fill.travel == null ? false : fill.travel.Value);

1 Comment

The actual name for this (a ? b : c) pattern is the ternary operator.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.