M
M
Michael2014-11-07 20:32:02
ASP.NET
Michael, 2014-11-07 20:32:02

The object reference does not point to an instance of the object, what could be the problem?

Good day to all, I started to study ASP.NET MVC, on Habrhabr I came across a series of useful lessons on ASP.NET MVC, I got up at one of the lessons: ASP NET.MVC Lesson 3. Working with the database (), I did everything according to the example, with the assembly is fine, during execution an exception of the type occurs:
System.NullReferenceException: The reference to the object does not point to an instance of the object.
998fa2323e0040afb3c1adb776cef669.png
In general, I think people write to this Ninject that the thing is necessary, I agree, but so far they are not in trouble with it.
I'll give you the code.
Repository:

public class  SQLRepository : IRepository
{   
 [Inject]
        public DBLDataContext _db { get; set; }
        public IQueryable<Role> Roles { get { return _db.Role; } }

        public bool CreateRole(Role instance)
        {
            if (instance.Id == 0)
            {
                _db.Role.InsertOnSubmit(instance);
                _db.Role.Context.SubmitChanges();
                return true;
            }
            return false;
        }
}

The interface itself:
public Interface IRepository
{
  IQueryable<Role> Roles { get; }
        bool CreateRole(Role instance);
        bool UpdateRole(Role instance);
        bool RemoveRole(int Id);
}

Controller:
[Inject]
        IRepository Repository { get; set; }
        public ActionResult Index()
        {         
            ViewBag.R= Repository.Roles.ToList();
            return View();//по примеру данные из таблицы "Роли" пробрасывались в переменную var и передавались в качестве аргумента метода View()
        }

I can’t understand what the trouble is ... I have been sitting for many hours and racking my brains over this.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2014-11-07
@MickMS

Here you go:

[NullReferenceException: Ссылка на объект не указывает на экземпляр объекта.]
   Test.Models.SQLRepository.get_Roles() in c:\Users\Михаил\Documents\Visual Studio 2013\Projects\Test\Test\Models\SQLRepository.cs:58
   Test.Controllers.HomeController.Index() in c:\Users\Михаил\Documents\Visual Studio 2013\Projects\Test\Test\Controllers\HomeController.cs:19
   lambda_method(Closure , ControllerBase , Object[] ) +62
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +211
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +48
   System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +57
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +223
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +48
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9657896
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

W
wkololo_4ever, 2014-11-07
@wkololo_4ever

Post a stack trace, there are several possible causes of the error.
Or the whole project.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question