Sealer_05
Sealer_05

Reputation: 5566

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 
 }

Upvotes: 61

Views: 155279

Answers (8)

How 'bout a Fresca
How 'bout a Fresca

Reputation: 2317

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;

Upvotes: 6

Cornelius J. van Dyk
Cornelius J. van Dyk

Reputation: 728

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

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

Upvotes: 1

StarPilot
StarPilot

Reputation: 2272

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

Upvotes: 1

Val Bakhtin
Val Bakhtin

Reputation: 1464

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;

Upvotes: 18

Oblivion2000
Oblivion2000

Reputation: 616

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.

Upvotes: 4

Adil
Adil

Reputation: 148120

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

Upvotes: 5

Tejs
Tejs

Reputation: 41236

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.

Upvotes: 77

Peter Ritchie
Peter Ritchie

Reputation: 35881

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

Upvotes: 7

Related Questions