Reputation: 67
I keep getting this error in line 146. I would appreciate anybody explaining this error to me.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 144: var courses = dc.Courses.ToList().
Line 145: Where(course =>
Line 146: course. <------??????
Line 147: User.
Line 148: Id ==
Line 149: currentUserId);
Source File: c:\aaa\vat\c#\proj\igoryen\igoryen\ViewModels\Repo_Course.cs Line: 146
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
igoryen.ViewModels.<>c__DisplayClassb.<getListOfCourseBaseAM>b__a(Course course) in c:\aaa\vat\c#\proj\igoryen\igoryen\ViewModels\Repo_Course.cs:146
System.Linq.WhereListIterator`1.MoveNext() +56
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +381
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
AutoMapper.Mappers.EnumerableMapperBase`1.Map(ResolutionContext context, IMappingEngineRunner mapper) +273
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +838
[AutoMapperMappingException:
Mapping types:
WhereListIterator`1 -> IEnumerable`1
System.Linq.Enumerable+WhereListIterator`1[[igoryen.Models.Course, igoryen, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.IEnumerable`1[[igoryen.ViewModels.CourseBase, igoryen, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Destination path:
IEnumerable`1
Source value:
System.Linq.Enumerable+WhereListIterator`1[igoryen.Models.Course]]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +936
AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType, Action`1 opts) +196
AutoMapper.MappingEngine.Map(Object source, Action`1 opts) +172
AutoMapper.MappingEngine.Map(Object source) +121
AutoMapper.Mapper.Map(Object source) +107
igoryen.ViewModels.Repo_Course.getListOfCourseBaseAM(String currentUserId) in c:\aaa\vat\c#\proj\igoryen\igoryen\ViewModels\Repo_Course.cs:151
igoryen.ViewModels.Repo_Course.getSelectListOfCourse(String currentUserId) in c:\aaa\vat\c#\proj\igoryen\igoryen\ViewModels\Repo_Course.cs:136
igoryen.Controllers.CancellationsController.Create() in c:\aaa\vat\c#\proj\igoryen\igoryen\Controllers\CancellationsController.cs:50
lambda_method(Closure , ControllerBase , Object[] ) +101
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +435
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60
System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +76
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +36
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +73
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3c() +117
System.Web.Mvc.Async.<>c__DisplayClass45.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3e() +323
System.Web.Mvc.Async.<>c__DisplayClass30.<BeginInvokeActionMethodWithFilters>b__2f(IAsyncResult asyncResult) +44
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
System.Web.Mvc.Async.<>c__DisplayClass28.<BeginInvokeAction>b__19() +72
System.Web.Mvc.Async.<>c__DisplayClass1e.<BeginInvokeAction>b__1b(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514928
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446
Well this is what I have for user:
// 1) Peter teaches IPC144
Course c = new Course();
c.CourseCode = "IPC144";
c.CourseName = "Introduction into programming";
c.RoomNumber = "1000";
c.RunTime = "1000";
c.Faculty = f;
c.User = UserPeter; // 17
f.Courses.Add(c); //
c.Students.Add(bob);
bob.Courses.Add(c);
dc.Courses.Add(c);
c = null;
Unfortunately, I can't post a screen shot here, but I am debugging with a breakpoint and I don't see any User
values that are null
.
I have these files:
Models/Initializer.cs
where I initialize all the lists/tables. ViewModels/Repo_Course.cs
where I have the method that does the query (which returns the error). The part I am not getting is this:
as I debug with breakpoints I see that:
Models/Initializer.cs
when the Course
gets initialized, User
has a value and is not null
. ViewModels/Repo_Course.cs
in line 146 (shown above), User
is null
! Why???UserPeter
is of type ApplicationUser
:
var UserPeter = new ApplicationUser();
string userPw8 = "123456";
var userInfo8 = new MyUserInfo() { FirstName = "Peter", LastName = "Peterson" };
UserPeter.UserName = "Peter";
UserPeter.HomeTown = "Toronto";
UserPeter.MyUserInfo = userInfo8;
var UserPeterCreate = UserManager.Create(UserPeter, userPw8);
if (UserPeterCreate.Succeeded) {
var addUserPeterToRole3Result = UserManager.AddToRole(UserPeter.Id, roleName3);
}
The ApplicationUser
class, it derives from IdentityUser
:
public class ApplicationUser : IdentityUser {
//[Key]
//public int Id { get; set; }
public string HomeTown { get; set; }
//public virtual ICollection<Cancellation> Cancellations { get; set; }
//public virtual ICollection<Course> Courses { get; set; }
public virtual MyUserInfo MyUserInfo { get; set; }
}
The MyUserInfo
class
public class MyUserInfo {
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
The IdentityUser
class (from metadata):
public class IdentityUser : IUser {
public IdentityUser();
public IdentityUser(string userName);
public virtual ICollection<IdentityUserClaim> Claims { get; }
public virtual string Id { get; set; }
public virtual ICollection<IdentityUserLogin> Logins { get; }
public virtual string PasswordHash { get; set; }
public virtual ICollection<IdentityUserRole> Roles { get; }
public virtual string SecurityStamp { get; set; }
public virtual string UserName { get; set; }
}
The Course
class:
public class Course { // 10
public Course() {
this.Faculty = new Faculty();
this.Students = new List<Student>();
}
[Key]
public int CourseId { get; set; }
public string CourseCode { get; set; }
public string CourseName { get; set; }
public string RoomNumber { get; set; }
public string RunTime { get; set; }
public Faculty Faculty { get; set; }
public List<Student> Students { get; set; }
public virtual ApplicationUser User { get; set; }
}
Upvotes: 1
Views: 2816
Reputation: 4992
If line 146 does the null access, it's more likely that your courses
list contains null
values than some course having a null
user.
Upvotes: 0