[.Net] Editor + SearchPanes leads to "Object reference not set to an instance of an object."
[.Net] Editor + SearchPanes leads to "Object reference not set to an instance of an object."
Hello everyone,
I recently switched the backend of a project from PHP to .Net for internal reasons, and it seems like that there is an issue with the .Net library when using the SearchPanes feature and the Editor with server side processing enabled.
Here is a short reduced case:
var editor = new Editor(db, "scraping_consult")
.Field(new Field("scraping_consult.id")
.Set(Field.SetType.Create)
)
.Field(new Field("scraping_consult.creation_date")
.Set(Field.SetType.Create)
)
.Field(new Field("scraping_consult.sector")
.SetFormatter(Format.IfEmpty(null))
.SearchPaneOptions(new SearchPaneOptions()
.Table("scraping_consult")
.Value("sector")
)
);
var result = editor.TryCatch(false)
.Debug(true)
.Process(Request.Form)
.Data();
For a reason that I can't figure, adding .SearchPaneOptions(new SearchPaneOptions() …) generates the following Exception:
System.NullReferenceException: Object reference not set to an instance of an object.
at DataTables.SearchPaneOptions.Exec(Field fieldIn, Editor editor, List`1 leftJoinIn, DtRequest http, Field[] fields)
at DataTables.Field.SearchPaneOptionsExec(Field field, Editor editor, List`1 leftJoin, Field[] fields, DtRequest http)
at DataTables.Editor._Get(Object id, DtRequest http)
at DataTables.Editor._Process(DtRequest data)
at DataTables.Editor.Process(DtRequest data)
at DataTables.Editor.Process(IEnumerable`1 data, String culture)
at asptest_noauth.Pages.ConsultModel.OnPost() in C:\Users\Vincent\asptest-noauth\Pages\Consult.cshtml.cs:line 207
at lambda_method26(Closure , Object , Object[] )
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory.ActionResultHandlerMethod.Execute(Object receiver, Object[] arguments)
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
I don't get any debug information as the server returns an error 500.
Any idea what am I doing wrong here?
Vincent.
Answers
Hi @Vincent.P ,
This isn't something that I have come across before. In the example that I have locally there is a field as follows.
So I'm not sure exactly why yours is not working. Even when I add the formatter that you have it works fine. Can you try and Identify which line in
SearchPaneOptions.Exec
is throwing the error?Thanks,
Sandy
Hi @sandy,
Here is the error with the line numbers:
Maybe I should also say that this is on an ASP.Net Razor pages project, so it might be the difference that makes it happen?
Best regards,
Vincent
Hi @Vincent.P ,
There are a couple of things that it could be on that line. Would you be able to link to a test case please so that I can see the request and response that are going to and from the server? Failing that could you paste them in here please?
Thanks,
Sandy
Hi @sandy,
The project isn't deployed on a server yet, but on my local machine, here are the request and response:
Request:
Request headers:
Response:
Can you let us know what version of the Editor .NET libraries you are using please? If not the current release of 2.0.2, it would be worth upgrading.
If that doesn't solve it, could you show us the full controller you are using with the Editor .NET initialisation so we can attempt to reproduce the error here. Unfortunately, we aren't seeing anything obvious in the code.
Thanks,
Allan
Hi @allan , I am using the latest version of the Editor (2.0.2).
I think I am also using the latest version of the js libraries (if it is also relevant).
Here you can find the controller that is used: https://gist.github.com/Ezneh/d6a9e02dc6676c00ca2c06a111f7745a
Keep in mind it is an ASP.Net MVC Razor application.
As an update: I also provided the javascript that is used to retrieve the fields as well
If you try to access a member of a class (here MyClass) then you get a System.NullReferenceException. Which is the same as "object reference not set to an instance of an object" . It indicates that you are trying to access member fields, or function types, on an object reference that points to null. That means the reference to an Object which is not initialized. To prevent the error, objects that could be null should be tested for null before being used.
if (mClass != null)
{
// Go ahead and use mClass
mClass.property = ...
}
else
{
// Whoops! mClass is null and cannot be used without first assigning it to an instance reference
// Attempting to use mClass here will result in NullReferenceException
}