Null Reference Error on mjoin

Null Reference Error on mjoin

kstankiewiczkstankiewicz Posts: 8Questions: 3Answers: 1

I am attempting to perform an mjoin to facilitate an uploadMany field. This is within a .NET MVC project. Basically I have these two tables in SQL:

EndorsementRequests:
-ID
-Name
-etc. etc.

EndorsementFiles:
-ID
-FilePath
-RequestID (FK)

Where RequestID is EndorsementRequests.ID foreign key. Each EndorsementRequest can have multiple files associated.

Whenever I try to do an mjoin I get the error "System.NullReferenceException: 'Object reference not set to an instance of an object.'"

Here's the mjoin being performed in the Controller:

using (var datatablesDB = new DataTables.Database(settings.Type, settings.ConnectionStringEndsTracker))
            {
                Editor editor = new Editor(datatablesDB, "EndorsementRequests")
                    .Model<EndorsementsViewModel>()
                    .MJoin(new MJoin("EndorsementFiles")
                    .Model<EndorsementFiles>()
                    .Link("EndorsementRequests.ID", "EndorsementFiles.RequestID")
                    )
                    //Other fields, etc. 

The EndorsementsViewModel and EndorsementFiles models contain the same fields found in both SQL tables.

I didn't include the part where you add a new field within the mjoin and run .Upload on it because I get the error even when that isn't included. If I remove the mjoin the editor functions normally

I have read the documentation and looked at the uploadMany .net example but maybe I am missing something. I tried using a link table that associates Requests and Files in a separate table as the documentation recommended, but I still get the same error

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Can you show me the code for your two models as well please?

    Thanks,
    Allan

  • kstankiewiczkstankiewicz Posts: 8Questions: 3Answers: 1

    Sure:

    namespace Intranet.Models
    {
        using System;
        using System.Collections.Generic;
        
        public partial class EndorsementFiles
        {
            public int ID { get; set; }
            public int RequestID { get; set; }
            public string FileName { get; set; }
            public string FilePath { get; set; }
        }
    }
    
    namespace Intranet.Models
    {
        using System;
        using System.Collections.Generic;
        
        public partial class EndorsementsViewModel
        {  
            public System.DateTime DateRequested { get; set; }
            public string UW { get; set; }
            public string UWA { get; set; }
            public string NamedInsured { get; set; }
            public string PolicyID { get; set; }
            public string Broker { get; set; }
            public string PolicyForm { get; set; }
            public bool Urgent { get; set; }
            public string RequestDescription { get; set; }
            public string Code { get; set; }
            public Nullable<System.DateTime> DateCompleted { get; set; }
            public string LegalPerson { get; set; }
            public string Status { get; set; }
            public string UserID { get; set; }
            public int ID { get; set; }
        }
    }
    

    These mirror the database fields:

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Thanks! I fear I don't know what is causing this (yet). Could you add .TryCatch(false) before the .Process(...) call please? That should let the exception show us some back trace as well. It would be useful to know where in the code the break is actually happening.

    Thanks,
    Allan

  • kstankiewiczkstankiewicz Posts: 8Questions: 3Answers: 1

    Sure, just to narrow it down I removed everything from the editor object but the mjoin-

    using (var datatablesDB = new DataTables.Database(settings.Type, settings.ConnectionStringEndsTracker))
                {
                    Editor editor = new Editor(datatablesDB, "EndorsementRequests")
                        .Model<EndorsementsViewModel>()
                        .MJoin(new MJoin("EndorsementFiles")
                        .Model<EndorsementFiles>()
                        .Link("EndorsementRequests.ID", "EndorsementFiles.RequestID"))
    .TryCatch(false)
                        .Debug(true);
    
    var reponse = editor.Process(formData).Data();
    }
    

    The NullReference error is thrown when the response object is generated. Here is the StackTrace field from the exception:

    "   at DataTables.MJoin.Data(Editor editor, DtResponse response)\r\n   at DataTables.Editor._Get(Object id, DtRequest http)\r\n   at DataTables.Editor._Process(DtRequest data)\r\n   at DataTables.Editor.Process(DtRequest data)\r\n   at DataTables.Editor.Process(NameValueCollection data)\r\n   at Intranet.Controllers.EndorsementsController.EndorsementsTable() in I:\\Git Projects\\Intranet\\Intranet\\Controllers\\EndorsementsController.cs:line 119\r\n   at lambda_method(Closure , ControllerBase , Object[] )\r\n   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)\r\n   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)\r\n   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)\r\n   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)\r\n   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)\r\n   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()\r\n   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)\r\n   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()\r\n   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass48.<InvokeActionMethodFilterAsynchronouslyRecursive>b__41()"
    

    Screenshot of exception-

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    We've been talking about this via e-mail and it would appear that the issue is Editor's assumption of a default primary key name of "id", but here it is actually "ID". Its slightly surprising that the error crops up in quite this way and I need to see if that can be improved, but just adding "ID" as a third parameter to the Editor constructor resolves this.

    Allan

This discussion has been closed.