Anonymous Type "Nullable object must have a value" Error

user3587754

I am using an anonymous type to represent an object so that I can convert it into JSON format using the Json() method. Here is a sample of my code below.

The "MoviesList" variable is just a list of objects of type Objects.Movie.

        var result = new List<object>();
        foreach (Objects.Movie movie in MoviesList)
        {
            result.Add(new
            {
                CompletedTime = (movie.CompletedTime == null ? new Nullable<DateTime>() : movie.CompletedTime),
                Genre = movie.Genre,
                Title = movie.Title,
                Director = movie.Director
            });
        }
        return Json(result, JsonRequestBehavior.AllowGet);

Before, I had the code written so that the CompletedTime property in the anonymous type is equal to the CompletedTime Property in the Movie object.

However, when I ran that and there were Movie objects in MoviesList where the CompletedTime Property was null (as the movie wasn't watched and therefore not finished yet), I got a System.InvalidOperationException saying: Nullable object must have a value.

So, I tried changing it to the code I have above, and I still get the same error.

I want a null value to be there if the movie has not been finished yet but I am getting this error. Is there any way to fix this or should I try a different approach?

movie.CompletedTime is of type "DateTime?"

EDIT: Whenever I debug this code, the debugger goes into the foreach loop, and iterates. However, when it is on an iteration where an Objects.Movie object contains the property CompletedTime (movie.CompletedTime) where the value is null, it catches an error and returns me to the JsonResult type method that called the above code and returns the caught error

StackTrace is below:

 at System.Nullable`1.get_Value()
   at WebService.Controllers.DataController.GenerateMovieList(DateTime date, String classID) in C:\WebService\Controllers\DataController.cs:line 212
   at WebService.Controllers.DataController.GetMovieList(String Date, String ClassID) in C:\WebService\Controllers\DataController.cs:line 133
Igor

After looking at your stack trace it does look like you are doing a .Value on a nullable object that does not have a value. Try changing that one line to this:

CompletedTime = movie.CompletedTime, // just pass in the nullable object as is

It would also help to include the complete methods as you have them if this is not the answer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Linq: Nullable Object must have Value

From Dev

Nullable object must have a value datetime

From Dev

Decimal? - Nullable object must have a value

From Dev

"Nullable object must have a value" when Where uses a method?

From Dev

best method to solve Nullable object must have a value in datetime

From Dev

Nullable object must have a value while using datepicker control with dropdown control

From Dev

"Nullable object must have a value" exception after checking for null on a non-primitive/non-struct object

From Dev

ASP.NET MVC 5 Razor throws 'Nullable object must have a value' even when the object has value

From Dev

Error converting LINQ anonymous type to IList<>

From Dev

Error converting LINQ anonymous type to IList<>

From Dev

Linq Query return Anonymous Type Error

From Dev

Linq GroupBy anonymous type error MVC4

From Dev

Anonymous Type and Null

From Dev

Anonymous Type Functions

From Dev

Accessing anonymous type variables

From Dev

List of properties to anonymous Type

From Dev

How to Session an anonymous type?

From Dev

Order by fields in an anonymous type

From Dev

Nested Anonymous Type in LINQ

From Dev

anonymous fields in type declaration?

From Dev

how to use an Anonymous Type

From Dev

nhibernate projection to anonymous type

From Dev

Sort on anonymous type in DBQuery

From Dev

Aggregate tasks in anonymous type

From Dev

Anonymous type function declaration

From Dev

Anonymous Type, and the great unknown

From Dev

Use variable in an anonymous type

From Dev

How to Session an anonymous type?

From Dev

Cartesian Product of Anonymous type