Null Reference Error on mjoin
Null Reference Error on mjoin
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
Can you show me the code for your two models as well please?
Thanks,
Allan
Sure:
These mirror the database fields:
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
Sure, just to narrow it down I removed everything from the editor object but the mjoin-
The NullReference error is thrown when the response object is generated. Here is the StackTrace field from the exception:
Screenshot of exception-
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