-1

This related to my previous post, even though it was original posted as this post it ran in to another problem to which it was answered. So to avoid complexity i have decided to re-post it with a confirmed single error!

i have the following Create() post method to save a user to mst_users table.

    [HttpPost]
    public ActionResult Create(CustomerVM custObject)
    {

        if ( ModelState.IsValid )
        {
                mst_users user = new mst_users 
                { 
                    uName=custObject.User, 
                    password=custObject.Password, 
                    dtCreated=DateTime.UtcNow, 
                    isLocked=false
                };

                db.mst_users.Add(user);
                db.SaveChanges();
             }
        }
}

when the methods executes at db.SaveChanges() it throws the error Object is not set an instance of an object but i have initialzed all required fields for the table but it shows me a field in a view that belongs to another collection but its not part of users table here is the video

here is the user object:

enter image description here

here is the error:

Object reference not set to an instance of an object.

Source of the error

Line 44:         </div>
    Line 45:         <div class="editor-field">
    Line 46:             @Html.DropDownListFor(model => model.NameTitle, Model.NameTitleColl)
    Line 47:             @Html.ValidationMessageFor(model => model.NameTitle)
    Line 48:         </div>

Here is the stack:

[NullReferenceException: Object reference not set to an instance of an object.]
   ASP._Page_Views_Customer_Create_cshtml.Execute() in c:\aspmvc4-test\test1\test1\Views\Customer\Create.cshtml:46
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +96
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +62
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +259
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +294
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +242
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +21
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +175
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +89
   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() +9651796
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
Phill Greggan
  • 2,234
  • 3
  • 20
  • 33
  • Is db properly initialized? Can you post all exception details? (message, call stack) – Alexei - check Codidact Dec 28 '15 at 13:35
  • did you try setting a breakpoint at the start of your method and stepping through the code? checking your locals window will show you the current values of the objects. Check for the one that is still null and set it accordingly. – sous2817 Dec 28 '15 at 13:35
  • also looking at your video, you have model.{something} and Model.{something}. the casing on your variables doesn't match...is that intentional (right around 30 seconds in)? – sous2817 Dec 28 '15 at 13:37
  • Possible duplicate of [why call to saveChanges() throws error "Object reference not set to an instance of an object."](http://stackoverflow.com/questions/34492533/why-call-to-savechanges-throws-error-object-reference-not-set-to-an-instance) – CodeCaster Dec 28 '15 at 13:39
  • @sous2817 if you take a look at my previous post it has details about this! but remember it ran to another problem to which it was answered! so i cannot delete the original post nor this! – Phill Greggan Dec 28 '15 at 13:43
  • @CodeCaster i have given an explaination is there way i could simplify the two posts – Phill Greggan Dec 28 '15 at 13:44
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – asawyer Dec 28 '15 at 13:51
  • @asawyer i have updated my post – Phill Greggan Dec 28 '15 at 13:52
  • @sous2817 i have updated my post – Phill Greggan Dec 28 '15 at 13:52
  • @PhillGreggan Please read the "What is a NullReferenceException" link, especially the `ASP.NET MVC empty view models` section as I would imagine this is most likely your issue. – asawyer Dec 28 '15 at 13:56
  • @asawyer i just can think the connection between the object null reference error and the passed value, even table definition does not even have any column that belongs to another table maybe im new to MVC but when all the required values been passed why it complains about object null reference – Phill Greggan Dec 28 '15 at 14:00
  • @PhillGreggan Shouldn't there be a return statement in there somewhere? – asawyer Dec 28 '15 at 14:19

2 Answers2

0

try to do something like this

mst_users user = new mst_users();
user.uName=custObject.User;
user.password=custObject.Password; 
user.dtCreated=DateTime.UtcNow;
user.isLocked=false;
db.mst_users.Add(user);
db.SaveChanges();
Anas Omar
  • 504
  • 3
  • 8
  • how this differ from my original code.. dtCreate should be assigned with date because custObject contains the valid date – Phill Greggan Dec 28 '15 at 13:45
0

the problem was solved, even when i tagged the db table dtUpdated as not null and assigned with getDate() i must send a date from client else throws the above error!

Phill Greggan
  • 2,234
  • 3
  • 20
  • 33